
/*

 :: Login  
GET /info.php?
jsonpcallback=jsonp1323389966359&_=1323389984249
&user=Greateload
&action=login
&pvp=0
&code=e7e8b613d1b08d069751171da8b0eb17fe02ee8c				// iphone kr 
&_l=kr
&_p=EW-IPHONE-KR HTTP/1.1



jsonp1323389966359(
	{"code":0,"ret":{
		"server":"http:\/\/k2.emrosswar.com\/",
		"user":"Greateload","referer":"feelcool","refercode":"Greateload"}})
		
		
		
GET /game/
login_api.php?
jsonpcallback=jsonp1323389966360&_=1323389985557
&username=Greateload
&password=1212
&_l=kr&_p=EW-IPHONE-KR HTTP/1.1
		
		
jsonp1323389966360(
	{"code":0,"ret":{"key":"b557990e3591188901cb50f1422bc0d4"}})
	
	
	=========================  when  restart  : from  getusetinfo  
GET /game/
get_userinfo_api.php?
jsonpcallback=jsonp1323389966361&_=1323389987156
&key=b557990e3591188901cb50f1422bc0d4
&_l=kr
&_p=EW-IPHONE-KR HTTP/1.1
			
jsonp1323389966361(
{"code":0,"ret":{
	"user":{"id":18949,"nick":"greateload","nationid":1,"level":42,"money":345,"guild":"greate","guildid":262,
		"gpower":1,"gflag":97,"protection":0,"p_end":1322486844,"status":0,
		"city":[
			{"id":22289,"name":"centercast","landtype":3,"x":61,"y":19},
			{"id":22336,"name":"heaven","landtype":4,"x":61,"y":18},
			{"id":22349,"name":"Spacecastl","landtype":2,"x":60,"y":17},
			{"id":22423,"name":"Forward","landtype":5,"x":62,"y":17}],
		"conq":[0,0,null,5],
		"gift":[{"id":168,"msg":"","s":0}],"pvp":0
		},
	"events":[
		{"id":1,"icon":"gem.jpg","name":"\uc131\uc2e4\ud55c \uad70\uc8fc\ub97c \uc704\ud55c \ubcf4\uc0c1! [5\/7 \uc77c\uac04]","desc":"<b>\uc624\ub298\uc758 \ubcf4\uc0c1\uc740<\/b> \ub85c\uadf8\uc778 \ud6c4 \ubc1b\uc744 \uc218 \uc788\uc2b5\ub2c8\ub2e4. 7\uc77c\uac04 \uc5f0\uc18d\uc73c\ub85c \ub85c\uadf8\uc778\ud558\uba74 \ub354 \uc2b9\ub9ac\ud560 \uc218 \uc788\uc744 \uac83\uc785\ub2c8\ub2e4.<b>\uac15\ub825\ud55c \uc601\uc9c0 \uad6c\ucd95\uc744 \uc704\ud558\uc5ec!<\/b> \ubb34\ub8cc!"},{"id":2,"icon":"0.png","name":"\ub808\ubca8 \uaca9\ucc28\ub294 \ud65c\ub3d9\uc801\uc778 \uad70\uc8fc\ub4e4\uc744 \ubcf4\ud638\ud569\ub2c8\ub2e4.","desc":"\uba3c\uc800 \uacf5\uaca9\ud558\uc9c0 \uc54a\ub294 \ud55c, 10\ub808\ubca8 \uc774\uc0c1\uc778 \uad70\uc8fc\ub4e4\ub85c\ubd80\ud130 \ubcf4\ud638\ubc1b\uc2b5\ub2c8\ub2e4. \uc774 \ubc84\ud504\ub294 24\uc2dc\uac04 \uc774\uc0c1 \uc624\ud504\ub77c\uc778\uc77c \uacbd\uc6b0 \uc801\uc6a9\ub418\uc9c0 \uc54a\uc2b5\ub2c8\ub2e4."}
		],
	"status":{"invite":1,"ver":0.9,"vs":1.25,"vr":0.5,"ce":1,"he":1}}})
	
	

 //  these are Quest list :  
GET /game/
system_task_api.php?
jsonpcallback=jsonp1323389987755&_=1323389988001&key=b557990e3591188901cb50f1422bc0d4
&action=task_list
&_l=kr&_p=EW-IPHONE-KR HTTP/1.1



jsonp1323389987755(
	{"code":0,"ret":
	{"quest":[
		{"id":46,"grade":0,"status":1,"done":0},		//  status : accepted ,  not done 
		{"id":47,"grade":0,"status":1,"done":0},
		{"id":55,"grade":0,"status":1,"done":0},
		{"id":27,"grade":0,"status":1,"done":0},
		{"id":28,"grade":0,"status":1,"done":0},
		{"id":29,"grade":0,"status":1,"done":0},
		{"id":48,"grade":0,"status":0,"done":1},		// status : 0,  done : done ... 
		{"id":49,"grade":0,"status":0,"done":1},
		{"id":50,"grade":0,"status":1,"done":0},
		{"id":51,"grade":0,"status":0,"done":0},		// status : not accepted : not done ( e-mail )
		{"id":52,"grade":0,"status":1,"done":0},
		{"id":53,"grade":0,"status":1,"done":0},
		{"id":54,"grade":0,"status":1,"done":0},
		{"id":38,"grade":0,"status":1,"done":1}]}})
		


GET /game/
get_cityinfo_api.php?jsonpcallback=jsonp1323389988302&_=1323389988326&key=b557990e3591188901cb50f1422bc0d4
&city=22289
&_l=kr&_p=EW-IPHONE-KR HTTP/1.1

jsonp1323389988302({"code":0,"ret":{
	"city":[61,180,153911,337500,37863,337500,2797910,337500,1065,337500,2503,2853,5,5,5,15,11,22,18,16,3,1,15,
	[
		{"id":44718,"itemid":166,"secs":344462},			//  cons bless !!!!  : itemid : 166 
		{"id":44893,"itemid":166,"secs":399762}
	],0],"grade":42,"money":345}})




GET /game/get_cdinfo_api.php?jsonpcallback=jsonp1323389987756&_=1323389991990&key=b557990e3591188901cb50f1422bc0d4&city=22289&_l=kr&_p=EW-IPHONE-KR HTTP/1.1

jsonp1323389987756({"code":0,"ret":{"cdlist":[
{"id":843558,"cdtype":1,"target":9,"owner":0,"secs":17539},    //  uni  
{"id":846316,"cdtype":1,"target":10,"owner":0,"secs":69911},	// arena 
{"id":847570,"cdtype":1,"target":13,"owner":0,"secs":26386},   	// fuci
{"id":220951,"cdtype":2,"target":7,"owner":87575,"secs":75168}, //   study  2 ex Arm  7  h id 
{"id":192011,"cdtype":3,"target":15,"owner":152,"secs":4111}    // train  o   3   gid  
],"grade":42,"money":345}})	
	



2nd c

jsonp1323389987757({"code":0,"ret":{
	"cdlist":[
	{"id":846725,"cdtype":1,"target":10,"owner":0,"secs":50767},  // Arena 
	{"id":847298,"cdtype":1,"target":9,"owner":0,"secs":81547},		// uni 
	{"id":847304,"cdtype":1,"target":8,"owner":0,"secs":70536},		//  barrack
	{"id":221157,"cdtype":2,"target":5,"owner":87847,"secs":128594}   // stuy  Eff train 
	],"grade":42,"money":345}})

* 
* * 
* */




	
"use strict";
	
function CTasks() {
	this.class_CTask = 1;
	this.objName = 'CTasks : ';

	this.actionSeq = new CActionSeq();
	
};		
	
CTasks.prototype = {
	
			// 	===================================================================
//       GEt Rumor 
		
	/*
		rumor 占쎈벡��

GET /game/
gen_conscribe_api.php?jsonpcallback=jsonp1323353477773&_=1323362108456&key=7c67cf5bf34d95dbf8c039a067a4619a
&city=22478
&action=rumors
&_l=en&_p=EW-DROID HTTP/1.1


jsonp1323353477773({"code":0,"ret":{"hero":[]}})
*/	

	/*
	checkConscribe: function(data){
			
			
		},*/
	
		// consider param is already set
	getRumor: function( citnum , owner, cb) {
		
		debug.log(this, 3, 'test here ');
		
		var outer = this;
		var rumor_action = {
			name: 'rumor',
			start: undefined,
			// param: rumor_cbs,		// for notify cb functions 
			action_table: [
				{ 
					cmd: 'rumor_conscribe',
					preAction:  undefined,
					doParam: { citnum: citnum, cons_action: 'rumors',  cb : function (data) {
									cb.call(owner, data); }
								},
					doFunc:	 'rumor_conscribe',
					gotResult: this.actionAResult,
				},
				],
			done_cb: undefined,			// cb for complete action 				
		};
	
		this.actionSeq.setAction(rumor_action);
		this.actionSeq.start();
 },
	
	
	
	
	
	
	
	
	
	task_cbs: { 
				success_cb : function(code, msg) {
					debug_log(" TASKS :: success CB is called !!! ") ;
				}, 
				error_cb : function(errorcode) {
					debug_log("TASKS  Erro CB is called !!! in test case :" + errorcode) ;
				},
				notify_cb : function(code, msg) {
					em.util.print_result("TASKS " + code + msg );
				}, 
	},
	
	
	
	
	// ================================================ 
	// task  with single action ,  with deffered  
	
			
	doBuild: function(dfd,  buildtype, ex_res) {
		var outer = this;
		
		// when done ex res 
		function done_ex_res_doBuild() {

			var citnum = em.cstlmgr.curCstlID();
			
			// use action helper
			var param = { citnum: citnum , 'buildtype':buildtype,   cb : function (data) {
									outer.checkBuild.call(outer, dfd, data); } };
			em.ahelper.build_api(param);	
		};
		
		if (ex_res.total != 0  || (ex_res.g2f ||  ex_res.g2w || ex_res.g2i)) {
			// exchange & wait .. 
			debug.log(this, ' exchange try : ', ex_res);
			this.exchangeRes( ex_res.g2f , ex_res.g2w, ex_res.g2i, done_ex_res_doBuild);
			
		} else {
			// do direct 
			done_ex_res_doBuild();
		}

	},
	
	// got result with cdlist 
	// update cstlmgr with cdlist  -> cstlmgr  will update it;s view  
	checkBuild: function(dfd, data ) {
					//	{"code":0,"ret":{
					// "cdlist":[{"id":847443,"cdtype":1,"target":"8","owner":0,"secs":521}]}})

				debug.log(this, 'this', this, 'got build ret :', data);
				
				// if  error , send error to view . => dfd fail() �먯꽌 泥섎━�섏옄. 
				if ( data.code != 0 ) {
					dfd.reject(data);
					return;
				} 

				// update ret cdlist 				
				em.cstlmgr.updateCdlist(data.ret.cdlist);

				dfd.resolve(data);
				
				return; // end 
	},
	
// 	===================================================================
//      Exchage res 

	exchangeRes: function( g2f_amount, g2w_amount, g2i_amount, done_cb) {
		var outer = this;
		var cstlinfo = em.cstlmgr.getCurCstl();
		
		if ( !g2f_amount && ! g2w_amount && !g2i_amount  ) {
			debug.log(this, 'invalid res request ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
			// return.. 
			done_cb();
			return;
		}
		
		var exchage_action = {
			name: 'exchangeRes',
			start: undefined,
			// param: cbs,		// for cb functions 
			action_table: [
				{ 
					cmd: 'localMarket',
					preAction:  undefined,
					doParam: { citnum: cstlinfo.id ,  cb : function(data) { outer.checkLocalMarket.call(outer, data); }
						},
					doFunc:	 'localMarket',
					gotResult: this.actionAResult,
				},
				{ 
					cmd: 'localMarket_resput',
					preAction:  undefined,
					doParam: { citnum: cstlinfo.id , g2f: g2f_amount,  g2w: g2w_amount, g2i: g2i_amount,
								cb : function(data) {  outer.checkLocalMarket_resput.apply(outer, [data, cstlinfo]); }
						},
					doFunc:	 'localMarket_resput',
					gotResult: this.actionAResult,		
				},		
				],
			done_cb: done_cb			// cb for complete action 
		};
	
		
		this.actionSeq.setAction(exchage_action);
		this.actionSeq.start();
	},
		
	

	checkLocalMarket: function(data) {
		debug.log(this, 1, "got checkLocalMarket :" , data );
		// this.exchangeFod_step2();
		var outer = this;
		setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(1900));		
	},
	
// jsonp1322895150033({"code":0,"ret":{"w":154679,"f":10822,"i":112768,"g":160144}})
	checkLocalMarket_resput: function(data, cstlinfo) {
		
		// update cstl  with ret res 
		em.cstlmgr.updateRes(data.ret);
		
		debug.log(this, "got checkLocalMarket_resput " , data );
		
		// do not anything.  after  this castlee cdinfo cb will start att
		var outer = this;
		setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(500));				
	},


/*
	================================
	buy gem item 
*/

	buyGemItem: function(  cat, id, done_cb) {
		var outer = this;
		var self = this;
		var cstlinfo = em.cstlmgr.getCurCstl();
		
		this.ctx_buyGemItem = {};
		
		var buyGemItem_action = {
			name: 'buyGemItem_action',
			action_table: [
			/*

sys_shop_api.php?jsonpcallback=jsonp1326014287325&_=1326016999493&key=0314fac3e037989c062370584d3dc99b
&action=list_goldshopitems
&type=1
&city=17736
&_l=en&_p=EW-DROID HTTP/1.1

jsonp1326014287325({"code":0,"ret":
{"item":[71,68,72,73,69,70],
"price":[45000,100000,30000,20000,80000,60000],
"attr":[
[0,0,0,9,0,0],[0,0,0,18,0,0],[0,0,0,6,0,0],[0,0,0,3,0,0],[0,0,0,15,0,0],[0,0,0,12,0,0]]}})


GET /game/
sys_shop2_api.php?jsonpcallback=jsonp1326014287326&_=1326017003408&key=0314fac3e037989c062370584d3dc99b
&action=list_baoshopitems
&page=1
&cat=5
&_l=en&_p=EW-DROID HTTP/1.1

jsonp1326014287326({"code":0,"ret":
{"item":[{"id":112,"p":3},
{"id":131,"p":20},{"id":166,"p":19},
{"id":167,"p":59},{"id":128,"p":20},{"id":184,"p":10},
{"id":162,"p":40},{"id":161,"p":4},{"id":195,"p":199},{"id":0,"p":0}],"max":1}})

GET /game/
sys_shop2_api.php?jsonpcallback=jsonp1326014287327&_=1326017009019&key=0314fac3e037989c062370584d3dc99b
&action=purchase
&num=1
&id=131
&_l=en&_p=EW-DROID HTTP/1.1

jsonp1326014287327({"code":0,"ret":{"money":0,"itemid":75644}})

		*/
				// shop 
				{ 
					cmd: 'sys_shop_api',
					doParam: { 'action' : 'list_goldshopitems', 'type': 1 ,	'citnum' : cstlinfo.id ,
								cb : function(data) {  		
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(500)); }
						},
					doFunc:	 'sys_shop_api',
				},	
				// go to tap 
				{ 
					cmd: 'sys_shop2_api',
					doParam: { 'action' : 'list_baoshopitems', 'page': 1 ,	'cat' : cat , 
								cb : function(data) {  		
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(1000)); }
						},
					doFunc:	 'sys_shop2_api',
				},	
				// purchase
				{ 
					cmd: 'sys_shop2_api',
					doParam: {  'action': 'purchase', 'num' : 1, 'id' : 131 , 
								cb : function(data) {  	
									if ( data.cod == 0 ) {
										// no need to update.. next cons will update  it self.. em.cstlmgr.updateHeroes(data.ret);
									}
									self.ctx_buyGemItem.result = data;
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(300)); }
						},
					doFunc:	 'sys_shop2_api',
				},	
				],
				done_cb:function () { 
							done_cb( self.ctx_buyGemItem.result );
					 },			// cb for complete action  	
		};
	
		this.actionSeq.setAction(buyGemItem_action);
		this.actionSeq.start();
	},
/*
	================================
	use item 
*/

	// if sid  is not 0 :  find id from sid type.  ( self first one. ) 
	useItem: function(  cat, sid,  id, num,  done_cb) {
		var outer = this;
		var self = this;
		var cstlinfo = em.cstlmgr.getCurCstl();
		
		debug.log(self, 'useItem : ', cat, 'sid:',  sid, 'id',  id, num,  done_cb );
		
		this.ctx_useItem = {};
		this.ctx_useItem.curpage = 1;
		var actions = {
			name: 'useItem',
			action_table: [
			/*
goods_api.php?jsonpcallback=jsonp1326014287249&_=1326014559916&key=0314fac3e037989c062370584d3dc99b
&action=goods_list
&type=3
&page=1
&_l=en&_p=EW-DROID HTTP/1.1


jsonp1326014287249({"code":0,"ret":{"max":1,
"item":[{"item":
{"id":75614,"sid":167,"num":1},"use":1,"sale":0},
{"item":{"id":75612,"sid":128,"num":1},"use":1,"sale":0},
{"item":{"id":75616,"sid":190,"num":1},"use":1,"sale":50000},
{"item":{"id":75609,"sid":168,"num":1},"use":1,"sale":0},
{"item":{"id":75615,"sid":188,"num":1},"use":2,"sale":0},
{"item":{"id":75611,"sid":126,"num":1},"use":1,"sale":0},
{"item":{"id":75606,"sid":169,"num":1},"use":1,"sale":0},
{"item":{"id":75613,"sid":161,"num":2},"use":1,"sale":0},
{"item":{"id":75610,"sid":80,"num":1},"use":1,"sale":0}]}})


GET /game/
goods_mod_api.php?jsonpcallback=jsonp1326014287250&_=1326014568812&key=0314fac3e037989c062370584d3dc99b
&action=use
&city=17736
&id=75614
&num=1
&_l=en&_p=EW-DROID HTTP/1.1

jsonp1326014287250({"code":0,"ret":
{"item":[],"gem":0,"buff":
{"id":6381,"itemid":167,"secs":2592000},"wood":0,"food":0,"iron":0,"gold":0}})

list one more : updtae cur page 

		*/
				// shop 
				{ 
					cmd: 'goods_api_item_list',
					doParam: { 'action' :'goods_list', 'type' :3 , 'page' : this.ctx_useItem.curpage,
								cb : function(data) {
									
									self.ctx_useItem.max = data.ret.max;  

									// get goods list ( my items )		
									// find sid = id . set  2nd action param with id  
									var want_id = 0;

									$.each ( data.ret.item , function(i, tem) {
										debug.log(self, 'Tem : ', tem , tem.item.sid, tem.item.id );
										
										//  find id from sid 
										if ( sid != 0 ) {
											debug.log(self, ' \t sid check : ', tem , tem.item.sid, sid );
											
											if ( tem.item.sid == sid ) {
												want_id = tem.item.id;
												return false; // exit each loop
											} 
										} 
										else 
										{
											if ( tem.item.id == id ) {
												want_id = id;
												return false ; // exit each loop
											}
										}
									});
									
									debug.log(self, 'try Find ID : with sid: ', sid,  'put id: ', id, ' find id', want_id);
									
									if ( want_id ) {
										self.actionSeq.setDoParam('goods_mod_api_use', {'id':want_id });
									} else {
										
										// search next page when exists next page  
											self.ctx_useItem.curpage++;
										if ( self.ctx_useItem.curpage <= self.ctx_useItem.max) {
											
											debug.log(self, ' Search Next page : ', self.ctx_useItem.curpage,  'max:', self.ctx_useItem.max);
											
											self.actionSeq.setDoParam('goods_api_item_list', {'page': self.ctx_useItem.curpage });
											self.actionSeq.retryCurAction('goods_api_item_list');
										} else {
											
											debug.log(self, ' Search Fail... not exist tem.: ', self.ctx_useItem.curpage,  'max:', self.ctx_useItem.max);
											// exit  this seq 
											done_cb( {code: -1} );
											return; 
										}
										
									}
									
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(600)); }
						},
					doFunc:	 'goods_api',
				},	
				// use tem 
				{ 
					cmd: 'goods_mod_api_use',
					doParam: { 'action': 'use' , 'citnum' : cstlinfo.id, 'id': id, 'num' : num, 
								cb : function(data) {  	
									// this is main action 
									self.ctx_useItem.result = data;	
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(200)); }
						},
					doFunc:	 'goods_mod_api',
				},	
				// update after action : list again 
				{ 
					cmd: 'goods_api_list_again',
					doParam: { 'action' :'goods_list', 'type' :3 , 'page' : 1,
								cb : function(data) {  
									// get goods list ( my items )		
									
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(0)); }
						},
					doFunc:	 'goods_api',
				},	
				],
				done_cb:function () { 
							done_cb( self.ctx_useItem.result );
					 },			// cb for complete action  	
		};
	
		this.actionSeq.setAction(actions);
		this.actionSeq.start();
	},
/*
	================================
	sale item 
*/

	// use item : same...   TODO : merge with useItem. 
	// if id == 0 , use sid. 
	saleItem: function(  type, sid,  id, num,  done_cb) {
		var outer = this;
		var self = this;
		var cstlinfo = em.cstlmgr.getCurCstl();
		
		this.ctx_saleItem = {};
		this.ctx_saleItem.curpage = 1;
		var actions = {
			name: 'saleItem',
			action_table: [
			/*
// sale  ref items 
&id=75618  , sid  115

GET /game/goods_mod_api.php?jsonpcallback=jsonp1326014287338&_=1326017118194&key=0314fac3e037989c062370584d3dc99b
&action=sale&city=17736
&id=75619			/./ sid 118 
&num=3
&_l=en&_p=EW-DROID HTTP/1.1

GET /game/goods_mod_api.php?jsonpcallback=jsonp1326014287342&_=1326017142967&key=0314fac3e037989c062370584d3dc99b
&action=sale&city=17736
&id=75620 , // sid 161
&num=3
&_l=en&_p=EW-DROID HTTP/1.1


jsonp1326014287342({"code":0,"ret":{"gold":40708}})

// update list again 
jsonp1326014287343({"code":0,"ret":
{"max":1,"item":[{"item":
{"id":75612,"sid":128,"num":1},"use":1,"sale":0},
{"item":{"id":75616,"sid":190,"num":1},"use":1,"sale":50000},
{"item":{"id":75615,"sid":188,"num":1},"use":2,"sale":0},
{"item":{"id":75611,"sid":126,"num":1},"use":1,"sale":0},
{"item":{"id":75613,"sid":161,"num":2},"use":1,"sale":0},
{"item":{"id":75610,"sid":80,"num":1},"use":1,"sale":0},
{"item":{"id":75617,"sid":153,"num":1},"use":1,"sale":0}]}})


list one more : updtae cur page 

		*/
				// shop 
				{ 
					cmd: 'goods_api_item_list',
					doParam: { 'action' :'goods_list', 'type' :3 , 'page' : this.ctx_saleItem.curpage,
								cb : function(data) {
									
									self.ctx_saleItem.max = data.ret.max;  
									// get goods list ( my items )		
									// find sid = id . set  2nd action param with id  
									
									var want_id = 0;
									$.each ( data.ret.item , function(i, tem) {
										// id is 0 
										if ( !id &&  tem.item.sid == sid ) {
											want_id = tem.item.id;
											return false;
										}
										
										if ( id && tem.item.id == id ) {
											want_id = id;
											return false ; // exit each loop
										}
									});
									
									debug.log(self, 'try Find ID : with sid: ', sid,  'put id: ', id, ' find id', want_id);
									
									if ( want_id ) {
										self.actionSeq.setDoParam('goods_mod_api_sale', {'id':want_id });
									} else {
										// search next page when exists next page  
											self.ctx_saleItem.curpage++;
										if ( self.ctx_saleItem.curpage <= self.ctx_saleItem.max) {
											debug.log(self, ' Search Next page : ', self.ctx_saleItem.curpage,  'max:', self.ctx_saleItem.max);

											self.actionSeq.setDoParam('goods_api_item_list', {'page': self.ctx_saleItem.curpage });
											self.actionSeq.retryCurAction('goods_api_item_list');
											
										} else {
											
											debug.log(self, ' Search Fail... not exist tem.: ', self.ctx_saleItem.curpage,  'max:', self.ctx_saleItem.max);
											// exit  this seq 
											done_cb( {code: -1} );
											return; 
										}
																				
									}
									
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(1000)); }
						},
					doFunc:	 'goods_api',
				},	
				// sale 
				/*
					&action=sale&city=17736
&id=75619			/./ sid 118 
&num=3
&_l=en&_p=EW-DROID HTTP/1.1
				*/
				{ 
					cmd: 'goods_mod_api_sale',
					doParam: { 'action': 'sale' , 'citnum' : cstlinfo.id, 'id': id, 'num' : num, 
								cb : function(data) {  		
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(300)); }
						},
					doFunc:	 'goods_mod_api',
				},	
				// update after action
				{ 
					cmd: 'goods_api',
					doParam: { 'action' :'goods_list', 'type' :3 , 'page' : 1,
								cb : function(data) {  
									// get goods list ( my items )		
									self.ctx_saleItem.result = data;
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(0)); }
						},
					doFunc:	 'goods_api',
				},	
				],
				done_cb:function () { 
							done_cb( self.ctx_saleItem.result );
					 },			// cb for complete action  	
		};
	
		this.actionSeq.setAction(actions);
		this.actionSeq.start();
	},
/*
	================================
	buy  item g shop  
*/

	// use item : same...   TODO : merge with useItem. 
	buyItem: function(  type,  id, done_cb) {
		var outer = this;
		var self = this;
		var cstlinfo = em.cstlmgr.getCurCstl();
		
		this.ctx_buyItem = {};
		this.ctx_buyItem.curpage = 1;
		var actions = {
			name: 'buyItem',
			action_table: [
			/*
/go to shop  : first type 
GET /game/sys_shop_api.php?jsonpcallback=jsonp1326014287354&_=1326017328757&key=0314fac3e037989c062370584d3dc99b
&action=list_goldshopitems
&type=1
&city=17736
&_l=en&_p=EW-DROID HTTP/1.1


jsonp1326014287354({"code":0,"ret":{
	"item":[71,68,72,73,69,70],"price":[45000,100000,30000,20000,80000,60000],
		"attr":[
		[0,0,0,9,0,0],[0,0,0,18,0,0],[0,0,0,6,0,0],[0,0,0,3,0,0],[0,0,0,15,0,0],[0,0,0,12,0,0]]}})


 //  ally token .. 
GET /game/
sys_shop_api.php?jsonpcallback=jsonp1326014287359&_=1326017380412&key=0314fac3e037989c062370584d3dc99b
&action=list_goldshopitems
&type=3
&city=17736
&_l=en&_p=EW-DROID HTTP/1.1

jsonp1326014287359({"code":0,"ret":{"item":[113,190],"price":[10000,100000],"attr":[[0,0,0,0,0,0],[0,0,0,0,0,0]]}})


// buy 
GET /game/
sys_shop_api.php?jsonpcallback=jsonp1326014287360&_=1326017396700&key=0314fac3e037989c062370584d3dc99b
&action=purchase
&type=3
&city=17736
&id=113
&_l=en&_p=EW-DROID HTTP/1.1

jsonp1326014287360({"code":0,"ret":{"gold":32708,"itemid":75656}})


		*/
				// shop 
				{ 
					cmd: 'sys_shop_api',
					doParam: { 'action' :'list_goldshopitems', 'type' :1 , 'citnum' : cstlinfo.id,
								cb : function(data) {
									
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(500)); }
						},
					doFunc:	 'sys_shop_api',
				},	
				// type 3,  all token 
				{ 
					cmd: 'sys_shop_api',
					doParam: { 'action': 'list_goldshopitems' , 'type' :type , 'citnum' : cstlinfo.id,
								cb : function(data) {  		
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(500)); }
						},
					doFunc:	 'sys_shop_api',
				},	
				// purchase
				{ 
					cmd: 'sys_shop_api',
					doParam: { 'action': 'purchase' , 'type' : type , 'citnum' : cstlinfo.id, 'id': id,
								cb : function(data) {  		
									self.ctx_buyItem.result = data;
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(0)); }
						},
					doFunc:	 'sys_shop_api',
				},	
				],
				done_cb:function () { 
							done_cb( self.ctx_buyItem.result );
					 },			// cb for complete action  	
		};
	
		this.actionSeq.setAction(actions);
		this.actionSeq.start();
	},
/*
	================================
	register trade 
*/

	// use item : same...   TODO : merge with useItem. 
	// not use sid.. 
	tradeReg: function( sid, id, num, price, done_cb) {
		var outer = this;
		var self = this;
		var cstlinfo = em.cstlmgr.getCurCstl();
		
		this.ctx_tradeReg = {};
		this.ctx_tradeReg.curpage = 1;
		var actions = {
			name: 'tradeReg',
			action_table: [
			/*
go to trade 
//GET /game/
safe_market_api.php?jsonpcallback=jsonp1326014287368&_=1326017537965&key=0314fac3e037989c062370584d3dc99b
&type=1
&page=1
&_l=en&_p=EW-DROID HTTP/1.1

jsonp1326014287368({"code":0,"ret":{"max":6,"item":
[[2704,73,20001,598190,[0,0,0,3,0,0]],[2690,113,4500005,581225],
[2688,88,50000,575561,[0,0,0,0,0,3]],[2687,94,40000,575552,[0,0,0,0,3,0]],
[2686,94,40000,575545,[0,0,0,0,3,0]],[2685,73,20000,575542,[0,0,0,3,0,0]],
[2684,79,25000,575533,[0,300,0,0,0,0]],[2683,94,40000,575528,[0,0,0,0,3,0]],
[2681,94,48000,561012,[0,0,0,0,3,0]],[2679,73,2500123,553576,[0,0,0,3,0,0]]]}})


// go to my list 
safe_goods_api.php?jsonpcallback=jsonp1326068330581&_=1326068551350&key=59f36d131858ffcb544278859bad306c
&action=list_invitem
&city=22289
&page=1
&_l=kr&_p=EW-IPHONE-KR HTTP/1.1

jsonp1326068330581({"code":0,"ret":{
	"item":[
	{"id":"575646","sid":"99","num":1,"attr":[0,0,60,0,0,0]},
	{"id":"577367","sid":"94","num":1,"attr":[0,0,0,0,3,0]},
	{"id":"580769","sid":"100","num":1,"attr":[0,0,30,0,0,0]},
	{"id":"580770","ctx_checkTrade":"100","num":1,"attr":[0,0,30,0,0,0]},
	{"id":"580772","sid":"94","num":1,"attr":[0,0,0,0,3,0]},
	{"id":"580774","sid":"93","num":1,"attr":[0,0,0,0,6,0]},
	{"id":"580779","sid":"93","num":1,"attr":[0,0,0,0,6,0]},
	{"id":"581660","sid":"99","num":1,"attr":[0,0,60,0,0,0]},
	{"id":"581662","sid":"94","num":1,"attr":[0,0,0,0,3,0]},
	{"id":"587113","sid":"78","num":1,"attr":[0,600,0,0,0,0]}],"max":7}})

	// page 2 
	safe_goods_api.php?jsonpcallback=jsonp1326068330582&_=1326068553692&key=59f36d131858ffcb544278859bad306c
	&action=list_invitem
	&city=22289
	&page=2
	&_l=kr&_p=EW-IPHONE-KR HTTP/1.1
	
	jsonp1326068330582({"code":0,"ret":{"item":[{"id":"587115","sid":"100","num":1,"attr":[0,0,30,0,0,0]},{"id":"587122","sid":"94","num":1,"attr":[0,0,0,0,3,0]},{"id":"587131","sid":"90","num":1,"attr":[0,0,0,0,15,0]},{"id":"587138","sid":"75","num":1,"attr":[0,1500,0,0,0,0]},{"id":"594451","sid":"94","num":1,"attr":[0,0,0,0,3,0]},{"id":"594452","sid":"99","num":1,"attr":[0,0,60,0,0,0]},{"id":"594453","sid":"93","num":1,"attr":[0,0,0,0,6,0]},{"id":"594454","sid":"79","num":1,"attr":[0,300,0,0,0,0]},{"id":"594456","sid":"94","num":1,"attr":[0,0,0,0,3,0]},{"id":"594459","sid":"78","num":1,"attr":[0,600,0,0,0,0]}],"max":7}})
	
	



// regi  item 
GET /game/
safe_goods_api.php?jsonpcallback=jsonp1326014287365&_=1326017475540&key=0314fac3e037989c062370584d3dc99b
&action=my_goods_safe
&city=17736
&id=75656
&safe_num=1
&price=310089
&_l=en&_p=EW-DROID HTTP/1.1

jsonp1326014287365({"code":0,"ret":""})




// check wait list 
GET /game/
safe_goods_api.php?jsonpcallback=jsonp1326014287370&_=1326017543503&key=0314fac3e037989c062370584d3dc99b
&action=list_auction_item
&type=will
&city=17736
&page=1
&_l=en&_p=EW-DROID HTTP/1.1

jsonp1326014287370({"code":0,"ret":{"item":[[2756,113,310089,832]],"max":1}})



// check trad list 
GET /game/safe_goods_api.php?jsonpcallback=jsonp1326014287371&_=1326017547712&key=0314fac3e037989c062370584d3dc99b
&action=list_auction_item
&type=
&city=17736
&page=1
&_l=en&_p=EW-DROID HTTP/1.1

jsonp1326014287371({"code":0,"ret":{"item":[],"max":0}})


		*/
				// go to trade 

				{ 
					cmd: 'safe_market_api',
					doParam: { 'type' : 1, 'page' : 1,
								cb : function(data) {
									// trade list .. 
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(300)); }
						},
					doFunc:	 'safe_market_api',
				},	
			// go to my list 
				{ 
					cmd: 'safe_goods_api_my_list',
					doParam: { 'action': 'list_invitem' , 'citnum' : cstlinfo.id , 'page': 1, 
								cb : function(data) {

// 									jsonp1326068330581({"code":0,"ret":{
	// "item":[
	// {"id":"575646","sid":"99","num":1,"attr":[0,0,60,0,0,0]},
		// {"id":"587113","sid":"78","num":1,"attr":[0,600,0,0,0,0]}],"max":7}})
									self.ctx_tradeReg.max = data.ret.max;  

									// get goods list ( my items )		
									// find sid = id . set  2nd action param with id  
									var want_id = 0;
									
									// !!! : no tem.item.xxx  ---  in trade : tem.xxx  !!!!
									$.each ( data.ret.item , function(i, tem) {
										// id is 0 
										if ( !id &&  tem.sid == sid ) {
											want_id = tem.id;
											return false;
										}
										
										if ( id && tem.id == id ) {
											want_id = id;
											return false ; // exit each loop
										}
									});
									
									debug.log(self, 'try Find ID : with sid: ', sid,  'put id: ', id, ' find id', want_id);
									
									if ( want_id ) {
										self.actionSeq.setDoParam('safe_goods_api_trade', {'id':want_id });
									} else {
										
										// search next page when exists next page  
											self.ctx_tradeReg.curpage++;
										if ( self.ctx_tradeReg.curpage <= self.ctx_tradeReg.max) {
											
											debug.log(self, ' Search Next page : ', self.ctx_tradeReg.curpage,  'max:', self.ctx_tradeReg.max);
											
											self.actionSeq.setDoParam('safe_goods_api_my_list', {'page': self.ctx_tradeReg.curpage });
											self.actionSeq.retryCurAction('safe_goods_api_my_list');
										} else {
											
											debug.log(self, ' Search Fail... not exist tem.: ', self.ctx_tradeReg.curpage,  'max:', self.ctx_tradeReg.max);
											// exit  this seq 
											done_cb( {code: -1} );
											return; 
										}
										
									}
 									  		
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(700)); }
						},
					doFunc:	 'safe_goods_api',
				},		
			// set price and register 
				{ 
					cmd: 'safe_goods_api_trade',
					doParam: { 'action': 'my_goods_safe' , 'citnum' : cstlinfo.id , 'id':  id, 'safe_num' : 1, 'price': price ,
								cb : function(data) {
									// register   	
// 										{"code":0,"ret":""})
									// if error , stop & call cb 
									if ( data.code != 0 ) {
										if ( data.code == 3608) { debug.log(self, 'Not Enough Money');}
										debug.log(self, 'Regitser ERROR!!!', id, price , data );
										data.step = 'register';
										self.ctx_tradeReg.result = data;
										done_cb(data);
										return;
									}
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(300)); }
						},
					doFunc:	 'safe_goods_api',
				},	
				// check wait list 
				{ 
					cmd: 'safe_goods_api_will_list',
					doParam: { 'action': 'list_auction_item' , 'type' : 'will' , 'citnum' : cstlinfo.id, 'page': 1,
								cb : function(data) {
									data.step = 'will';
									// self.ctx_tradeReg.willlist.result = data;
// 									{"code":0,"ret":{"item":[[2756,113,310089,832]],"max":1}})
									if ( ! self.ctx_tradeReg.willlist ) {
										self.ctx_tradeReg.willlist = {};
										self.ctx_tradeReg.willlist.result = [];
										self.ctx_tradeReg.willlist.max = data.ret.max;  
										self.ctx_tradeReg.willlist.found = false;
										self.ctx_tradeReg.willlist.curpage = 1;
									} 
									self.checkTradeWillList( data, self.ctx_tradeReg.willlist  , sid );

									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(1000)); }
						},
					doFunc:	 'safe_goods_api',
				},	
				//  check trad list 
				{ 
					cmd: 'safe_goods_api_trade_list',
					doParam: { 'action': 'list_auction_item' , 'type' : '' /* &type= */, 'citnum' : cstlinfo.id, 'page': 1,
								cb : function(data) {  		
									// done trading success 
									 self.ctx_tradeReg.code = 0;
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(0)); }
						},
					doFunc:	 'safe_goods_api',
				},	
				],
				done_cb:function () { 
							done_cb( self.ctx_tradeReg  );
					 },			// cb for complete action  	
		};
	
		this.actionSeq.setAction(actions);
		this.actionSeq.start();
	},
/*
	================================
	check tem list trade 
*/

	// sid : 0 : get all list 
	checkTrade: function(  sid,  done_cb) {
		var outer = this;
		var self = this;
		var cstlinfo = em.cstlmgr.getCurCstl();
		
		this.ctx_checkTrade = {};

		var actions = {
			name: 'checkTrade',
			action_table: [
				// go to trade 
				{ 
					cmd: 'safe_market_api',
					doParam: { 'type' : 1, 'page' : 1,
								cb : function(data) {
									// trade list ..  :  get 
									self.ctx_checkTrade.marget = data;
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(500)); }
						},
					doFunc:	 'safe_market_api',
				},	
			// go to my list 
				{ 
					cmd: 'safe_goods_api_my_list',
					doParam: { 'action': 'list_invitem' , 'citnum' : cstlinfo.id , 'page': 1, 
								cb : function(data) {
									// get  list - all page  
									
// 									jsonp1326068330581({"code":0,"ret":{
									// "item":[
									// {"id":"575646","sid":"99","num":1,"attr":[0,0,60,0,0,0]},
										// {"id":"587113","sid":"78","num":1,"attr":[0,600,0,0,0,0]}],"max":7}})
		
									if ( !self.ctx_checkTrademytradelistt ) {
										self.ctx_checkTrade.mylist = {};
										self.ctx_checkTrade.mylist.result = [];
										self.ctx_checkTrade.mylist.max = data.ret.max;  
										self.ctx_checkTrade.mylist.found = false;
										self.ctx_checkTrade.mylist.curpage = 1;
									} 
								
									self.ctx_checkTrade.mylist.result[ self.ctx_checkTrade.mylist.curpage -1] = data;
									
									// find id  in list  : id is sid !!! 
									$.each ( data.ret.item , function(i, tem) {
										if ( sid && tem.sid == sid ) {
											self.ctx_checkTrade.mylist.found = true;
											return false ; // exit each loop
										}
									});
									
									debug.log(self, 'try Find ID :  id: ', sid, ' find id', self.ctx_checkTrade.mylist.found);
									
									// not found . try next page 
									if ( ! self.ctx_checkTrade.mylist.found ) {
										
										// search next page when exists next page  
										self.ctx_checkTrade.mylist.curpage++;
										if ( self.ctx_checkTrade.mylist.curpage <= self.ctx_checkTrade.mylist.max) {
											
											debug.log(self, ' Search Next page : ', self.ctx_checkTrade.mylist.curpage,  'max:', self.ctx_checkTrade.mylist.max);
											
											self.actionSeq.setDoParam('safe_goods_api_my_list', {'page': self.ctx_checkTrade.mylist.curpage });
											self.actionSeq.retryCurAction('safe_goods_api_my_list');
										} else {
											
											debug.log(self, ' Search Fail... not exist tem.: ', self.ctx_checkTrade.mylist.curpage,  'max:', self.ctx_checkTrade.mylist.max);
											// go next 
											// fall through.
										}
									}
 									  		
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(500)); }
						},
					doFunc:	 'safe_goods_api',
				},		
				// check wait list 
				{ 
					cmd: 'safe_goods_api_will_list',
					doParam: { 'action': 'list_auction_item' , 'type' : 'will' , 'citnum' : cstlinfo.id, 'page': 1,
								cb : function(data) {
// 									{"code":0,"ret":{"item":[[2756,113,310089,832]],"max":1}})
										if ( ! self.ctx_checkTrade.willlist ) {
											self.ctx_checkTrade.willlist = {};
											self.ctx_checkTrade.willlist.result = [];
											self.ctx_checkTrade.willlist.max = data.ret.max;  
											self.ctx_checkTrade.willlist.found = false;
											self.ctx_checkTrade.willlist.curpage = 1;
										} 
											self.checkTradeWillList( data, self.ctx_checkTrade.willlist , sid);
									// get  list - all page  
	
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(500)); }
						},
					doFunc:	 'safe_goods_api',
				},	
				//  check trad list 
				{ 
					cmd: 'safe_goods_api_mytrade_list',
					doParam: { 'action': 'list_auction_item' , 'type' : '' /* &type= */, 'citnum' : cstlinfo.id, 'page': 1,
								cb : function(data) {  		
									// get  list - all page  
									
									if ( !self.ctx_checkTrade.mytradelist ) {
										self.ctx_checkTrade.mytradelist = {};
										self.ctx_checkTrade.mytradelist.result = [];
										self.ctx_checkTrade.mytradelist.max = data.ret.max;  
										self.ctx_checkTrade.mytradelist.found = false;
										self.ctx_checkTrade.mytradelist.curpage = 1;
									} 
								
									self.ctx_checkTrade.mytradelist.result[ self.ctx_checkTrade.mytradelist.curpage -1] = data;
									
									// find id  in list  : id is sid 
									$.each ( data.ret.item , function(i, tem) {
										if ( sid && tem[1] == sid ) {
											self.ctx_checkTrade.mytradelist.price  = tem[2];
											self.ctx_checkTrade.mytradelist.found = true;
											return false ; // exit each loop
										}
									});
									
									debug.log(self, 'try Find ID :  id: ', sid, ' find id', self.ctx_checkTrade.mytradelist.found);
									
									// not found . try next page 
									if ( ! self.ctx_checkTrade.mytradelist.found ) {
										
										// search next page when exists next page  
											self.ctx_checkTrade.mytradelist.curpage++;
										if ( self.ctx_checkTrade.mytradelist.curpage <= self.ctx_checkTrade.mytradelist.max) {
											
											debug.log(self, ' Search Next page : ', self.ctx_checkTrade.mytradelist.curpage,  'max:', self.ctx_checkTrade.mytradelist.max);
											
											self.actionSeq.setDoParam('safe_goods_api_will_list', {'page': self.ctx_checkTrade.mytradelist.curpage });
											self.actionSeq.retryCurAction('safe_goods_api_will_list');
										} else {
											
											debug.log(self, ' Search Fail... not exist tem.: ', self.ctx_checkTrade.mytradelist.curpage,  'max:', self.ctx_checkTrade.mytradelist.max);
											// go next 
											// fall through.
										}
									}
									
									//set result ok 
									self.ctx_checkTrade.code = 0;
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(300)); }
						},
					doFunc:	 'safe_goods_api',
				},	
				],
				done_cb:function () { 
							done_cb( self.ctx_checkTrade );
					 },			// cb for complete action  	
		};
	
		this.actionSeq.setAction(actions);
		this.actionSeq.start();
	},
	
	// find trade tem in will list & return .. result 
	checkTradeWillList: function(data, ctx , sid ) {
// 									{"code":0,"ret":{"item":[[2756,113,310089,832]],"max":1}})

								
									ctx.result[ ctx.curpage -1] = data;
									
									// find id  in list  : id is sid 
									$.each ( data.ret.item , function(i, tem) {
										if ( sid && tem[1] == sid ) {
											ctx.found = true;
											ctx.price  = tem[2];
											ctx.remainsec = tem[3];
											return false ; // exit each loop
										}
									});
									
									debug.log(self, 'try Find ID :  id: ', sid, ' find id', ctx.found);
									
									// not found . try next page 
									if ( ! ctx.found ) {
										
										// search next page when exists next page  
											ctx.curpage++;
										if ( ctx.curpage <= ctx.max) {
											
											debug.log(self, ' Search Next page : ', ctx.curpage,  'max:', ctx.max);
											
											self.actionSeq.setDoParam('safe_goods_api_will_list', {'page': ctx.curpage });
											self.actionSeq.retryCurAction('safe_goods_api_will_list');
										} else {
											
											debug.log(self, ' Search Fail... not exist tem.: ', ctx.curpage,  'max:', ctx.max);
											// go next 
											// fall through.
										}
									}
	},
/*
	================================
	Buy SP 30 tem 
*/

	// sid : 0 : get all list 
	buySP30: function( sid, type, price, step3_price, done_cb) {
		var outer = this;
		var self = this;
		var cstlinfo = em.cstlmgr.getCurCstl();
		
		this.ctx_buySP30 = {};

		var actions = {
			name: 'checkTrade',
			action_table: [
				// go to trade 
				{ 
					cmd: 'safe_market_api',
					doParam: { 'type' : 1, 'page' : 1,
								cb : function(data) {
									
									/*
										go to trade 
//GET /game/
safe_market_api.php?jsonpcallback=jsonp1326014287368&_=1326017537965&key=0314fac3e037989c062370584d3dc99b
&type=1
&page=1
&_l=en&_p=EW-DROID HTTP/1.1

jsonp1326014287368({"code":0,"ret":{"max":6,"item":
[[2704,73,20001,598190,[0,0,0,3,0,0]],[2690,113,4500005,581225],
[2688,88,50000,575561,[0,0,0,0,0,3]],[2687,94,40000,575552,[0,0,0,0,3,0]],
[2686,94,40000,575545,[0,0,0,0,3,0]],[2685,73,20000,575542,[0,0,0,3,0,0]],
[2684,79,25000,575533,[0,300,0,0,0,0]],[2683,94,40000,575528,[0,0,0,0,3,0]],
[2681,94,48000,561012,[0,0,0,0,3,0]],[2679,73,2500123,553576,[0,0,0,3,0,0]]]}})
									*/
									// trade list ..  :  get 
									
									if ( !self.ctx_buySP30.tradelist ) {
										self.ctx_buySP30.tradelist = {};
										self.ctx_buySP30.tradelist.result = [];
										self.ctx_buySP30.tradelist.max = data.ret.max;  
										self.ctx_buySP30.tradelist.found = false;
										self.ctx_buySP30.tradelist.curpage = 1;
									} 
								
									self.ctx_buySP30.tradelist.result[ self.ctx_buySP30.tradelist.curpage -1] = data;
									
									// find id  in list  : id is sid !!! 
									$.each ( data.ret.item , function(i, tem) {
										if ( sid && tem[1] == sid && 
											(  ( tem[2] % 10000)     ==  11  // xx0089 
											|| 
											(tem[2] == price  || tem[2] == step3_price )
											)) {
											self.ctx_buySP30.tradelist.found = true;
											self.ctx_buySP30.tradelist.id = tem[0];
											self.ctx_buySP30.tradelist.price = tem[2];
											
											self.actionSeq.setDoParam('safe_market_api_purchase', {'id':self.ctx_buySP30.tradelist.id} );
											return false ; // exit each loop
										}
									});
									
									debug.log(self, 'try Find ID :  id: ', sid, ' find id', self.ctx_buySP30.tradelist.found);
									
									// not found . try next page 
									if ( ! self.ctx_buySP30.tradelist.found ) {
										
										// search next page when exists next page  
										self.ctx_buySP30.tradelist.curpage++;
										if ( self.ctx_buySP30.tradelist.curpage <= self.ctx_buySP30.tradelist.max) {
											
											debug.log(self, ' Search Next page : ', self.ctx_buySP30.tradelist.curpage,  'max:', self.ctx_buySP30.tradelist.max);
											
											self.actionSeq.setDoParam('safe_market_api', {'page': self.ctx_buySP30.tradelist.curpage });
											self.actionSeq.retryCurAction('safe_market_api');
										} else {
											
											debug.log(self, ' Search Fail... not exist tem.: ', self.ctx_buySP30.tradelist.curpage,  'max:', self.ctx_buySP30.tradelist.max);
											// go next 
											// fall through.
										}
									}									
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(1000)); }
						},
						

					doFunc:	 'safe_market_api',
				},	
						/*
							//  	
/safe_market_api.php?jsonpcallback=jsonp1325476678156&_=1325550214658&key=362def183bdae036dc927ebc7d02600a
&action=purchasing
&city=15584
&id=17301
&_l=en&_p=EW-IPHONE-EN HTTP/1.1

jsonp1325476678163({"code":0,"ret":""})
						*/
				{ 
					cmd: 'safe_market_api_purchase',
					doParam: { 'action' : 'purchasing', 'citnum' : cstlinfo.id , 'id' : 0, // <<- re put 
								cb : function(data) {
									if ( data.code != 0 ) {
										debug.log( self, 'Done!!!: Purchase Tem : ' , self.ctx_buySP30.tradelist.id , 
										self.ctx_buySP30.tradelist.price );
									}
									//set final result 
									self.ctx_buySP30.result = data;
									self.ctx_buySP30.code = data.code;
									
									
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(300)); }
						},
					doFunc:	 'safe_market_api',
				},	
				],
				done_cb:function () { 
							done_cb( self.ctx_buySP30 );
					 },			// cb for complete action  	
		};
	
		this.actionSeq.setAction(actions);
		this.actionSeq.start();
	},
/*
	================================
	Hire H 
*/

	hireH: function(  done_cb) {
		var outer = this;
		var self = this;
		var cstlinfo = em.cstlmgr.getCurCstl();
		
		this.ctx_hireH = {};
		
		var hire_action = {
			name: 'hire_action',
			start: undefined,
			action_table: [
			/*
							GET /game/gen_conscribe_api.php?jsonpcallback=jsonp1326014287299&_=1326016401057&key=0314fac3e037989c062370584d3dc99b
			&city=17736
			&_l=en&_p=EW-DROID HTTP/1.1

			GET /game/gen_conscribe_api.php?jsonpcallback=jsonp1326014287300&_=1326016404004&key=0314fac3e037989c062370584d3dc99b
			&city=17736
			&action=pub_process&_l=en&_p=EW-DROID HTTP/1.1

			GET /game/gen_conscribe_api.php?jsonpcallback=jsonp1326014287301&_=1326016407209&key=0314fac3e037989c062370584d3dc99b
			&city=17736
			&action=hire_process
			&_l=en&_p=EW-DROID HTTP/1.1

			*/
				// go to area 
				{ 
					cmd: 'gen_conscribe_api',
					doParam: { citnum: cstlinfo.id , 
								cb : function(data) {  		
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(1000)); }
						},
					doFunc:	 'gen_conscribe_api',
					gotResult: this.actionAResult,		
				},	
				// pub_process
				{ 
					cmd: 'gen_conscribe_api', 
					doParam: { citnum: cstlinfo.id , 'action': 'pub_process', 
								cb : function(data) {  		
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(1000)); }
						},
					doFunc:	 'gen_conscribe_api',
					gotResult: this.actionAResult,		
				},	
				// hire_process
				/*
					jsonp1326014287301({"code":0,"ret":{"gold":537,"hero":{"id":7143,"gid":179,"p":10,"i":17,"c1":14,"f":50,"g":1,"c2":231,"fy":0,"s":0,"e":"10","w":0,"tw":0,"tl":0}}})
				*/
				{ 
					cmd: 'gen_conscribe_api', 
					doParam: { citnum: cstlinfo.id , 'action': 'hire_process', 
								cb : function(data) {  	
									if ( data.cod== 0 ) {
										// no need to update.. next cons will update  it self.. em.cstlmgr.updateHeroes(data.ret);
									}
									self.ctx_hireH.result = data;
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(0)); }
						},
					doFunc:	 'gen_conscribe_api',
					gotResult: this.actionAResult,		
				},	
				],
				done_cb:function () { 
							done_cb( self.ctx_hireH.result );
					 },			// cb for complete action  	
		};
	
		this.actionSeq.setAction(hire_action);
		this.actionSeq.start();
	},

// 	===================================================================
//      train 
/*
		
.. move camp

GET /game/
soldier_educate_api.php?jsonpcallback=jsonp1323353477782&_=1323362536045&key=7c67cf5bf34d95dbf8c039a067a4619a
&city=22478
&_l=en&_p=EW-DROID HTTP/1.1

jsonp1323353477782({"code":0,"ret":{"soldiers":[[1,100,1],[2,43,1],[3,0,0],[4,0,0],[5,0,0],[6,0,0]],
"space":2857,"head":703,"def":1,"next":[7,7],"f":-140}})

// max �좎룞�쇿뜝�댢딁뙼占폺linet �띠룊�숋옙�ｌ뫒亦낉옙. 
// hero 占썩뫁伊볩옙�띕Ь�좑옙
GET /game/
gen_conscribe_api.php?jsonpcallback=jsonp1323353477783&_=1323362646324&key=7c67cf5bf34d95dbf8c039a067a4619a
&city=22478
&action=gen_list
&extra=1&_l=en&_p=EW-DROID HTTP/1.1

jsonp1323353477783({"code":0,"ret":{"hero":[
{"id":87588,"gid":185,"p":9,"i":27,"c1":12,"f":51,"g":6,"c2":407,"fy":0,"s":0,"e":8,"w":0,"tw":0,"tl":1,"ex":2000,"te":2267,"np":0,"ni":0,"nc1":0,"nc2":0,"ns":0,"ncd":0,"pr":18000}]}})

 // 嶺뚮ㅄ維곩퐲占�

GET /game/
soldier_educate_api.php?jsonpcallback=jsonp1323353477784&_=1323362708911&key=7c67cf5bf34d95dbf8c039a067a4619a
&city=22478
&action=soldier_educate
&soldier=2
&num=323
&gen=185 // h gid 
&_l=en&_p=EW-DROID HTTP/1.1

//"cdtype":3, train

jsonp1323353477784({"code":0,"ret":{"cdlist":[{"id":191927,"cdtype":3,"target":2,"owner":185,"secs":9516}]}})
*/
	trainSol: function(cstl_idx , trs, ex_res, done_cb) {
		var outer = this;
		var self = this;
		// var userinfo = CDB.getCurUserInfo();
		// var cstlinfo = userinfo.castles[ cstl_idx ];
		var cstlinfo = em.cstlmgr.getCurCstl();
		
		if ( ! trs.type || ! trs.num ){
			debug.log(this, 3, 'Invalid TRoopsssssss');
			return false;
		}

		this.ctx_trainSols = {};
		
		var g2f_amount = ex_res.g2f,   g2w_amount =ex_res.g2w,  g2i_amount = ex_res.g2i;
		
		var train_action = {
			name: 'trainSol',
			start: undefined,
			action_table: [
			
				// exchange  res 
				{ 
					cmd: 'localMarket',
					preAction:  undefined,
					doParam: { citnum: cstlinfo.id ,  cb : function(data) { outer.checkLocalMarket.call(outer, data); }
						},
					doFunc:	 'localMarket',
					gotResult: this.actionAResult,
				},
				{ 
					cmd: 'localMarket_resput',
					preAction:  undefined,
					doParam: { citnum: cstlinfo.id , g2f: g2f_amount,  g2w: g2w_amount, g2i: g2i_amount,
								cb : function(data) {  outer.checkLocalMarket_resput.apply(outer, [data, cstlinfo]); }
						},
					doFunc:	 'localMarket_resput',
					gotResult: this.actionAResult,		
				},	


				//  train !! 
				{ 
					cmd: 'soldier_educate_camp',
					preAction:  undefined,
					doParam: { citnum: cstlinfo.id ,  cb : function(data) { 
															outer.checkSolCamp.apply(outer,[data, cstlinfo, trs]); }
						},
					doFunc:	 'soldier_educate_camp',
					gotResult: this.actionAResult,
				},
				{ 
					cmd: 'conscribe',
					preAction:  function(){
									// param valid check 
/*
									if ( ! trs.type || ! trs.num ){
										return false;
									}*/

									return true;
								},
					doParam: { citnum: cstlinfo.id , 'action': 'gen_list',   extra: 1, 
								cb : function(data) {  outer.checkConscribe.call(outer, data); }
						},
					doFunc:	 'conscribe',
					gotResult: this.actionAResult,		
				},	
				{ 
					cmd: 'soldier_educate_train',
					preAction:  undefined,
					doParam: { citnum: cstlinfo.id , 'action': 'soldier_educate',
								sol: trs.type,  num: trs.num, gid: 0, 
								cb : function(data) {  outer.checkSolTrain.call(outer, data); }
						},
					doFunc:	 'soldier_educate_train',
					gotResult: this.actionAResult,		
				},	

				],
				done_cb:function () { 
							done_cb( self.ctx_trainSols.result );
					 },			// cb for complete action  	
		};
	
			
		// check no need cxchange res 
		if ( !g2f_amount && ! g2w_amount && !g2i_amount  ) {
			debug.log(this, 'res request all 0 :: skip !! exchnage ');
			train_action.action_table = train_action.action_table.slice(2);
		
		}
		this.actionSeq.setAction(train_action);
		this.actionSeq.start();
	},
	
		/*
			jsonp1323353477782({"code":0,"ret":{"soldiers":[[1,100,1],[2,43,1],[3,0,0],[4,0,0],[5,0,0],[6,0,0]],
"space":2857,"head":703,"def":1,"next":[7,7],"f":-140}})
		*/
	// cal space !!! for sols : cal trainable sol num 
	checkSolCamp: function(data, cstlinfo, trs) {
		debug.log(this, 1, "got checkSolCamp :" , data , 'cstl', cstlinfo);
		if ( data.code !== 0) {
			em.util.print_result('Error  checl C=Sol Camp');
		//	this.error_cb(1);	// sol error
		} else {
			
			// update solders , space, head 
			$.extend( cstlinfo.sols, data.ret.soldiers);
			
			// calulate resorece !!!
			var per_res =  CDB.SOLS.COST[trs.type];
			var need_res = {};
			$.each( Object.keys(per_res) , function (i, item) { 
				need_res[item] = per_res[item] * trs.num; 
				});
			
			console.log( ' per_res ' , per_res);
			console.log( ' need_res ' , need_res);
			
			$.each( need_res, function(_, p) {
				var avail_num = trs.num;
				if ( need_res[p] > 0 &&  need_res[p] > cstlinfo.res[p]) {
					avail_num = Math.floor(need_res[p]  / per_res[p])
				}
				// get min num
				if ( avail_num < trs.num ) {
					trs.num = avail_num;
				} 
			});

		console.log( 'calced min num : ' + trs.num , 'space: ', data.ret.space,  ' head: ', data.ret.head);
	
		// calulate trs number  with space of cstl  , and head ( population )
		if ( trs.num > data.ret.space || trs.num > data.ret.head){
			trs.num = Math.min(data.ret.space, data.ret.head );
			em.util.print_result('change tr num by space ' + trs.num );
			console.log(data.ret);
		}
		
		this.actionSeq.setDoParam('soldier_educate_train', {'num':trs.num});
		// test !!!
		// this.actionSeq.setDoParam('soldier_educate_train', {'num': '2'});
		// this.actionSeq.setDoParam('soldier_educate_train', {'sol': 18});
		
		
		}

		var outer = this;
		setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(700));		
	},
	
	
	/*
jsonp1323353477783({"code":0,"ret":{"hero":[
{"id":87588,"gid":185,"p":9,"i":27,"c1":12,"f":51,"g":6,"c2":407,"fy":0,"s":0,"e":8,"w":0,"tw":0,"tl":1,"ex":2000,"te":2267,"np":0,"ni":0,"nc1":0,"nc2":0,"ns":0,"ncd":0,"pr":18000}]}})

	*/
	//  choose highest p hero.. 
	//!!1 data is h arr 
	checkConscribe: function(Harr) {
		debug.log(this, 1, "got checkConscribe :" , Harr );
		
		// if ( data.code !== 0 ) {
			// em.util.print_result('Error  checkConscribe Camp');
			// this.error_cb(2);	// checkConscribe error
		// }
		
		//  choose highest p hero..
		Harr.sort(function (a,b) { 
				return b.p - a.p; });
		
		//set param for next action 
		if ( Harr.length !== 0){
			this.actionSeq.setDoParam('soldier_educate_train', {'gid':Harr[0].gid});
		}
		
		var outer = this;
		setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(500));		
	},
	
	/*
		jsonp1323353477784({"code":0,"ret":{"cdlist":[{"id":191927,"cdtype":3,"target":2,"owner":185,"secs":9516}]}})
	*/
	// add cstl cd list !!! 
	checkSolTrain: function(data) {
		debug.log(this, 1, "got checkSolTrain :" , data );
		if ( data.code != 0 ){
			debug.log( this, 'ERROR train sol..', data);
			if ( data.code == 2307 ) { debug.log(this, 'No Space in BARR'); }
			// TODO : notify View the result .. not alert 
			// alert('ERROR train sol' + data.code);
			// go next to finish task :  return;
		} else {
			// update ret cdlist 				
			em.cstlmgr.updateCdlist(data.ret.cdlist);
		}
		this.ctx_trainSols.result = data;
		var outer = this;
		setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(400));		
	},










// disband
/*
soldier_educate_api.php?jsonpcallback=jsonp1325725020011&_=1325725041979&key=152ca40a2927f8e42b8ceed4872ee885
&city=10956
&action=disband
&soldier_num1=82*/

	// trs : { s: nnn , type : x , num: x}
	disbandSols: function ( trs, done_cb) {

		var outer = this;
		var self = this;
		var cstlinfo = em.cstlmgr.getCurCstl();
		this.ctx_disbandSols = {};
		
		var actions = {
			name: 'disband',
			start: undefined,
			action_table: [
				//  train !! 
				{ 
					cmd: 'soldier_educate_camp',
					preAction:  undefined,
					doParam: { citnum: cstlinfo.id ,  cb : function(data) {
									// update cstl 
									em.cstlmgr.updateSols( data.ret.soldiers); 
									
									// check sol tye & num 
									/*
													jsonp1323353477782({"code":0,"ret":{"soldiers":[[1,100,1],[2,43,1],[3,0,0],[4,0,0],[5,0,0],[6,0,0]],
"space":2857,"head":703,"def":1,"next":[7,7],"f":-140}})
									*/
									// adjust num of sol
									var exist_wantSolInfo = CUtil.findElArrByValPos( data.ret.soldiers , trs.type , 0 );
									
									// no trs in barr  : abort !! 
									if ( ! exist_wantSolInfo ) {
										debug.log( self, ' No wanted Trs in Barr' , trs.typs ,data.ret.soldiers );
										if ( done_cb)  {
												done_cb();
										}
									}
									
									var exist_solnum = exist_wantSolInfo[1];
									if ( trs.num > exist_solnum ) {
										trs.num = exist_solnum;
									}
									self.actionSeq.setDoParam('soldier_educate_disband', {'trs':{s: trs.num } });
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(1800));	
															}
						},
					doFunc:	 'soldier_educate_camp',
				},
				{ 
					cmd: 'soldier_educate_disband',
					preAction:  undefined,
					doParam: { 'citnum': cstlinfo.id , 'action': 'disband',
								troops: trs,  //trs.type,  num: trs.num,   
								cb : function(data) { 
									self.ctx_disbandSols.result = data;
									if (data.code != 0 ) {
										debug.log( self, ' Error in disbandSols ' , data);
									}
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(300));
						 		}
							},
					doFunc:	 'soldier_educate_disband',		// << use train fn 
				},	

				],
				done_cb:function () { 
							done_cb( self.ctx_disbandSols.result );
					 },			// cb for complete action  	
		};
	
		this.actionSeq.setAction(actions);
		this.actionSeq.start();	
		
	},








// 	===================================================================
//      Study  
/*

effi traing study

uni  
GET /game/
study_api.php?jsonpcallback=jsonp1323353477768&_=1323361529176&key=7c67cf5bf34d95dbf8c039a067a4619a
&city=22478&_l=en&_p=EW-DROID HTTP/1.1

jsonp1323353477768({"code":0,"ret":[[1,1,1],[2,1,1],[3,1,1],[4,1,1],[5,0,1]],"ext":[6,6]})

GET /game/gen_conscribe_api.php?jsonpcallback=jsonp1323353477766&_=1323361375218&key=7c67cf5bf34d95dbf8c039a067a4619a
&city=22478
&action=gen_list
&extra=1&_l=en&_p=EW-DROID HTTP/1.1

jsonp1323353477766({"code":0,"ret":{
"hero":[{"id":87588,"gid":185,"p":9,"i":27,"c1":12,"f":50,"g":6,"c2":407,"fy":0,"s":0,"e":10,"w":0,"tw":0,"tl":0,"ex":1069,"te":2267,"np":0,"ni":0,"nc1":0,"nc2":0,"ns":0,"ncd":0,"pr":18000}]}})

GET /game/study_mod_api.php?jsonpcallback=jsonp1323353477767&_=1323361379226&key=7c67cf5bf34d95dbf8c039a067a4619a
&city=22478&tech=5
&owner=87588
&_l=en&_p=EW-DROID HTTP/1.1

jsonp1323353477767({"code":0,"ret":{
"cdlist":[{"id":221306,"cdtype":2,"target":5,"owner":87588,"secs":907}]}})
// 餓λ쵌而숋옙占퐑ser info 揶쏉옙議뉐첎占�

		
*/
	// study type  : no lv. provide . just up !! 
	doStudy: function( dfd, study_type,   ex_res) {
		var outer = this;
		var self = this;
		var cstlinfo = em.cstlmgr.getCurCstl();
	
		this.study = {};
		this.study.study_type = study_type;
		
		if (ex_res.total != 0  || (ex_res.g2f ||  ex_res.g2w || ex_res.g2i)) {
			// exchange & wait .. 
			debug.log(this, ' exchange try : ', ex_res);
			this.exchangeRes( ex_res.g2f , ex_res.g2w, ex_res.g2i, done_ex_res_doStudy);
			
		} else {
			// do direct 
			done_ex_res_doStudy();
		}

		// when done ex res 
		function done_ex_res_doStudy() {

			// var citnum = em.cstlmgr.curCstlID();
			var train_action = {
					name: 'doStudy',
					start: undefined,
					// param: this.task_cbs,		// for cb functions 
					action_table: [
					/*
						GET /game/
study_api.php?jsonpcallback=jsonp1323353477768&_=1323361529176&key=7c67cf5bf34d95dbf8c039a067a4619a
&city=22478&_l=en&_p=EW-DROID HTTP/1.1

jsonp1323353477768({"code":0,"ret":[[1,1,1],[2,1,1],[3,1,1],[4,1,1],[5,0,1]],"ext":[6,6]})
					*/
						//   go to uni :  get study_  
						{ 
							cmd: 'study_api',
							preAction:  undefined,
							doParam: { citnum: cstlinfo.id ,  cb : function(data) { 
															outer.checkStudy.call(outer,data); }
								},
							doFunc:	 'study_api',
						},
						
						/*
							GET /game/gen_conscribe_api.php?jsonpcallback=jsonp1323353477766&_=1323361375218&key=7c67cf5bf34d95dbf8c039a067a4619a
&city=22478
&action=gen_list
&extra=1&_l=en&_p=EW-DROID HTTP/1.1


						*/
						// choose H 
						{ 
							cmd: 'conscribe',
							preAction: undefined,
							doParam: { citnum: cstlinfo.id , 'action': 'gen_list',   extra: 1, 
										cb : function(data) {  outer.checkConscribeStudy.call(outer, data); }
								},
							doFunc:	 'conscribe',
						},	
						
						/*
							GET /game/study_mod_api.php?jsonpcallback=jsonp1323353477767&_=1323361379226&key=7c67cf5bf34d95dbf8c039a067a4619a
&city=22478
&tech=5
&owner=87588
&_l=en&_p=EW-DROID HTTP/1.1

jsonp1323353477767({"code":0,"ret":{
"cdlist":[{"id":221306,"cdtype":2,"target":5,"owner":87588,"secs":907}]}})
						*/
						{ 
							cmd: 'study_mod_api',
							preAction:  undefined,
							doParam: { citnum: cstlinfo.id , 'tech': study_type,		// skill
										owner: 0 , 	// <- will be filled by conscribe 
										cb : function(data) {  outer.checkStudyMod.call(outer, data); }
								},
							doFunc:	 'study_mod_api',
						},		
					],
					
					done_cb: function () { 
							dfd.resolve(self.study.result);
					 },			// cb for complete action 						
				};
			
				self.actionSeq.setAction(train_action);
				self.actionSeq.start();
		};
		
	
	},
	
		/*
jsonp1323353477768({"code":0,"ret":[[1,1,1],[2,1,1],[3,1,1],[4,1,1],[5,0,1]],"ext":[6,6]})
		*/
	// check req skill & lv is valid : 
	checkStudy: function(data, cstlinfo) {
		var self  = this;
		
		debug.log(this, 1, "got checkStudy :" , data , 'cstl', cstlinfo);
		
		if ( data.code !== 0) {
			em.util.print_result('Error  checkStudy');
			//alert(' Error Get studies');
			
		} 
		else {
			var studies = data.ret;
			
			em.cstlmgr.updateStudies(studies);
			
			// check req skill & lv
			var reqest_valid = false;
			$.each( studies, function(i,s){
				if ( s[0] == self.study.study_type && s[1] == 1 ) {
					reqest_valid  = true;
					debug.log( self, ' Wanted Study is Possible !!');
					return false ; //exit each loop 
				}	
			}) ;
			
			if ( reqest_valid == false ) { debug.log( this, ' New Study ::', this.study.study_type );}
		}
		
		
		var outer = this;
		setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(900));		
	},
	
	
	/*
jsonp1323353477766({"code":0,"ret":{
"hero":[{"id":87588,"gid":185,"p":9,"i":27,"c1":12,"f":50,"g":6,"c2":407,"fy":0,"s":0,"e":10,"w":0,"tw":0,"tl":0,"ex":1069,"te":2267,"np":0,"ni":0,"nc1":0,"nc2":0,"ns":0,"ncd":0,"pr":18000}]}})

	*/
	//  choose highest i hero.. 
	//!! data is h arr 
	checkConscribeStudy: function(Harr) {
		debug.log(this, 1, "got checkConscribeStudy :" , Harr );
		
		//  choose highest p hero..
		Harr.sort(function (a,b) { 
				return b.i - a.i; });
		
		//set param for next action 
		if ( Harr.length !== 0){
			this.actionSeq.setDoParam('study_mod_api', {'owner':Harr[0].gid});
		}
		
		var outer = this;
		setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(700));		
	},
	
	/*
jsonp1323353477767({"code":0,"ret":{
"cdlist":[{"id":221306,"cdtype":2,"target":5,"owner":87588,"secs":907}]}})
	*/
	// add cstl cd list !!! 
	checkStudyMod: function(data) {
		debug.log(this, 1, "got checkStudyMod :" , data );
		
		if ( data.code == 0 ) {
			// update cdlist 
			em.cstlmgr.updateCdlist(data.ret.cdlist);
		}
		
		// save result for result dfd
		this.study.result = data;
		
		var outer = this;
		setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(300));		
	},


	// use  FastBld, FastSty.. 
	// fast id :  fast type id.  118 : 15m sty . 
	fastActionTem: function(  type, fastid, cdtype, target  , done_cb ) {
		var outer = this;
		var self = this;
		var cstlinfo = em.cstlmgr.getCurCstl();
		
		var fast_sids, action_cmd;
		if ( type == 1 ) {
			fast_sids = '115%2C116%2C117';
			action_cmd = 'build';
		}
		else if ( type ==  2 ) {
			fast_sids = '118%2C122%2C165';
			action_cmd = 'study';
		}
		
		this.ctx_fastAction = {};
		
		var actions = {
			name: 'fastAction',
			action_table: [
				// shop 
				{ 
					cmd: 'get_cdinfo_api',
					doParam: { citnum: cstlinfo.id ,
								cb : function(data) {
									/*
										sonp1326636913121({"code":0,"ret":{"
cdlist":[
{"id":911200,"cdtype":1,"target":9,"owner":0,"secs":566}
,{"id":911214,"cdtype":1,"target":10,"owner":0,"secs":2221},
{"id":911216,"cdtype":1,"target":8,"owner":0,"secs":83}],"grade":6,"money":0}})
									*/
									// find cd  with :  cdtype, & target
									
									var cd_id = 0;
									var secs = 0;
									$.each ( data.ret.cdlist , function(i, cd) {
										if ( cdtype == cd.cdtype &&  target == cd.target ) {
											cd_id = cd.id;
											secs = cd.secs;
											self.ctx_fastAction.cdid = cd.id;
											self.ctx_fastAction.secs = cd.secs;
											return false;	// exit each 
										}
									});
									
									debug.log(self, 'try Find cd  with cdtype: ', cdtype,  ' target ', target, ' found  cd id', cd_id);
									
									if ( cd_id ) {
										self.actionSeq.setDoParam('api_getcdprice', {'secs':secs });
										self.actionSeq.setDoParam('get_cdinfo_api_use_fast', {'tid':cd_id });
									} else {
											// no target... cd  return.. 
											debug.log(self, ' ~~~~~~~~~~~~~~~~~~~~ Search Fail... not exist cd  ');
											// exit  this seq 
											done_cb( {code: -1} );
											return; 
										}
									
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(500)); }
						},
					doFunc:	 'getCDInfo',
				},	
				// get cd price  
				/*
GET /game/api_getcdprice.php?jsonpcallback=jsonp1326636913122&_=1326638589486
&type=1
&secs=551
&_l=en&_p=EW-DROID HTTP/1.1

jsonp1326636913122({"code":0,"ret":{"price":1}})
				*/
				{ 
					cmd: 'api_getcdprice',
					doParam: { 'type':  type, 'secs' :  0 , 	// << - set by prev cdlist  
								cb : function(data) {  		
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(500)); }
						},
					doFunc:	 'api_getcdprice',
				},	
				// use fast item. 
				/*
					goods_api.php?jsonpcallback=jsonp1326636913123&_=1326638590467&key=956c18c1901b0c79e46a4c3fa37f61c9
&ids=115%2C116%2C117
&_l=en&_p=EW-DROID HTTP/1.1

jsonp1326636913123({"code":0,"ret":{"item":[{"id":680978,"sid":115,"num":3}]}})

				*/
				{ 
					cmd: 'goods_api_get_fast_inven',
					doParam: { 'ids' : fast_sids, 
								cb : function(data) {  
									// get fast sid  
									if ( data && data.ret && data.ret.item && data.ret.item[0] && data.ret.item[0].sid
											&& fastid ==  data.ret.item[0].sid ) {
										
										self.actionSeq.setDoParam('get_cdinfo_api_use_fast', {'iid':data.ret.item[0].sid });
									} else {
										// stop .. not fast tem in inven // exit  this seq 
											done_cb( {code: -1} );
											return; 
									}
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(1000)); }
						},
					doFunc:	 'goods_api_fast',
				},	
				/*
					GET /game/get_cdinfo_api.php?jsonpcallback=jsonp1326636913124&_=1326638596070&key=956c18c1901b0c79e46a4c3fa37f61c9
&city=23063
&tid=911200
&action=build
&iid=115
&_l=en&_p=EW-DROID HTTP/1.1

jsonp1326636913124({"code":0,"ret":{"secs":0}})
				*/
				{ 
					cmd: 'get_cdinfo_api_use_fast',
					doParam: { 'citnum': cstlinfo.id ,  'tid' : 0  , 'action' :  action_cmd, 'iid': 0 ,	// < tid ( cd id ), iid: fast tem sid < -- set by prev action.
								cb : function(data) {  
									
									// !! important !!! : update result secs. 
									self.ctx_fastAction.secs = data.ret.secs;
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(200)); }
						},
					doFunc:	 'get_cdinfo_ap_use_fasti',
				},	
				// re get cd list 
				{ 
					cmd: 'get_cdinfo_api',
					doParam: { citnum: cstlinfo.id ,
								cb : function(data) {  
									
									self.ctx_fastAction.code = data.code;
									
									// update cd list to cstl mgr 
									em.cstlmgr.updateCdlist(data.ret.cdlist);
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(0)); }
						},
					doFunc:	 'getCDInfo',
				},	
				],
				done_cb:function () { 
							done_cb( self.ctx_fastAction );
					 },			// cb for complete action  	
		};
	
		this.actionSeq.setAction(actions);
		this.actionSeq.start();
	},



/*
		get cstl info :   cdinfo. cit info, study..  
*/

	getCstlInfos: function(cstlinfo, done_cb) {
		var outer = this;
		var self = this;

		debug.log( this, 'getCstlInfos : cstlinfo id : ' + cstlinfo.id );
		
		this.ctx_cstlInfos = {};
		
		var actions = {
			name: 'getCstlInfos',
			action_table: [
			
				{
					cmd: 'getCitInfo',
					doParam: { citnum: cstlinfo.id ,  
								cb : function(data) {  outer.checkCitInfo.call(outer, data); }
						},
					doFunc:	 'getCitInfo',
				},
				
				{ 
					cmd: 'get_cdinfo_api',
					doParam: { citnum: cstlinfo.id ,
								cb : function(data) {  
									self.ctx_cstlInfos = data;
									// update cd list to cstl mgr 
									em.cstlmgr.updateCdlist(data.ret.cdlist);
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(300)); }
						},
					doFunc:	 'getCDInfo',
				},			
				{
					cmd: 'get_study_api',
					doParam: { citnum: cstlinfo.id ,
								cb : function(data) {  
									// update  
									em.cstlmgr.updateStudies(data.ret);
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(0)); }
						},
					doFunc:	 'study_api',					
				},		
				],
				done_cb:function () { 
							done_cb( self.ctx_cstlInfos );		// cd info result ::  always true;;
					 },			// cb for complete action  	
		};
	
		this.actionSeq.setAction(actions);
		this.actionSeq.start();		
		
	},
	
	


	 
	 
	applyAlly: function(  done_cb ) {
		var outer = this;
		var self = this;
		
		this.ctx_applyAlly = {};
		
		var actions = {
			name: 'applyAlly',
			action_table: [
/*
		go to ally menu 
GET /game/api_union.php?jsonpcallback=jsonp1326205755618&_=1326205897076&key=a34882ecc701a8d83ccc9e9e50e747e8
&page=1
&_l=en&_p=EW-DROID HTTP/1.1


*/			
				{ 
					cmd: 'goto_ally_menu',
					doParam: { 'page' : 1 ,
								cb : function(data) {
									/*
jsonp1326205755618({"code":0,"ret":
{"guild":[{"id":103,"name":"\uba85\uc608",
"intro":"\ue030\uba85\ud06c\ue030  \ud788\ud3ec\uba54\ub4e0\uc11c\ubc84 \ub7ad\ud0b91\uc704 Happy new year",
"flag":127,"score":14543078,"num":79,"max":150,"state":1},
{"id":6,"name":"\uace0\uad6c\ub824","intro":"\uc804\uc7c1\uc740 \ub9d0\ub85c \ud558\ub294\uac8c \uc544\ub2c8\ub2e4 \ub2e4\ub9cc \ubcd1\ub825\uc73c\ub85c \ud560\ubfd0","flag":11,"score":8981423,"num":140,"max":150,"state":1},{"id":116,"name":"\uc5d4\uc824","intro":"\ucd08\ubcf4\ubd84\ud658\uc601 \uc2a4\ud30c\uc774\ub294 \uc0ac\uc808^^  \ub3d9\ub9f9\uc6d0 \uac74\ub4e4\uba74 \ucca0\uc800\ud55c \ubcf4\ubcf5 \ub300\uae30\uc911!!","flag":34,"score":4556862,"num":143,"max":250,"state":1},{"id":88,"name":"\ud3c9\ud654","intro":"\ubc18\uaca9O \uc695\uc124X \u2728\ub3d9\ub9f9\ud569\ubcd1 \uc0c1\ub2f4 \ud658\uc601\ud569\ub2c8\ub2e4 \n\u2728\uc911\ub9bd\ub3d9\ub9f9\u2728\uc120\uacf5\uae08\uc9c0\u2728","flag":104,"score":3449267,"num":116,"max":150,"state":1},{"id":92,"name":"\ubb34\uc778\ub3c4","intro":"\uc5f0\ud569\uc911 \uc801\uadf9\uc801\uc778 \uc0ac\ub78c\uc744 \uc120\ud638\ud569\ub2c8\ub2e4 \ub9ce\uc774\ub9ce\uc774 \uac00\uc785\ud574 \uc8fc\uc138\uc694","flag":64,"score":1092854,"num":48,"max":150,"state":1},{"id":36,"name":"Chronous","intro":"Chronous\ub294\uc601\uc6d0\ud558\ub2e4\u2665 \ucd08\ubcf4\uc790\ub9e4\uc77c 60\ub9c8\ub10c \uc9c0\uae09\ub3d9\ub9f9\uc2e0\uccad\ud6c4 \uba54\uc77c \ubcf4\ub0b4\uc138\uc5ec \uc57d\uad00\uc787\uc74c","flag":127,"score":745828,"num":71,"max":150,"state":1},{"id":28,"name":"\uc218\uc9c0","intro":"\ud654\uc774\ud305!!","flag":47,"score":580649,"num":89,"max":150,"state":1},{"id":41,"name":"[WIN]_KING","intro":"\ue11d\uc6b0\ub9b0 \uc544\uc9c1 \uc8fd\uc9c0 \uc54a\uc558\ub2e4!!!\ue11d","flag":69,"score":479028,"num":40,"max":50,"state":1},{"id":244,"name":"\uce74\uc624\uc2a4","intro":"\"chaos\" \uc2dc\ub300\uac00 \uc774\uc81c \uc2dc\uc791\ud569\ub2c8\ub2e4","flag":87,"score":404076,"num":31,"max":50,"state":1},{"id":67,"name":"Outsider","intro":"\ud63c\uc790\uc11c \uc870\uc6a9\ud788","flag":123,"score":230667,"num":23,"max":50,"state":1}],"max":16}})
									*/
							// go next 
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(500)); }
						},
					doFunc:	 'api_union',
				},	
				/*
// search greate 

GET /game/api_union.php?jsonpcallback=jsonp1326205755619&_=1326205907627&key=a34882ecc701a8d83ccc9e9e50e747e8
&page=1
&name=greate
&_l=en&_p=EW-DROID HTTP/1.1

jsonp1326205755619({"code":0,"ret":
{"guild":[{"id":262,"name":"greate","intro":"","flag":97,"score":10208,"num":13,"max":50,"state":1}],"max":1}})

				*/
				{ 
					cmd: 'search_ally',
					doParam: { 'page': 1, 'name': 'extinctor',  
								cb : function(data) {  		
									// get id : 
									debug.log( self, 'serach ally : ', data);
									self.ctx_applyAlly.id = data.ret.guild[0].id ;
									self.actionSeq.setDoParam('apply_ally', {'id': self.ctx_applyAlly.id });
									self.actionSeq.setDoParam('submit_ally', {'id': self.ctx_applyAlly.id });
									
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(500)); } 
									
						},
					doFunc:	 'api_union',
				},	
				/*
// apply 
GET /game/api_union.php?jsonpcallback=jsonp1326205755620&_=1326205918230&key=a34882ecc701a8d83ccc9e9e50e747e8
&id=262
&_l=en&_p=EW-DROID HTTP/1.1

jsonp1326205755620({"code":0,"ret":
{"team":[{"id":1193,"name":"\uad70\ub2e8"},{"id":1194,"name":"\uad70\ub2e8"},
{"id":1195,"name":"\uad70\ub2e83"}]}})
				*/
				{ 
					cmd: 'apply_ally',
					doParam: { 'id' : 262, 
								cb : function(data) {  
									// set tid  ( team id )
									self.ctx_applyAlly.tid = data.ret.team[0].id ;
									self.actionSeq.setDoParam('submit_ally', {'tid': self.ctx_applyAlly.tid });
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(1000)); }
						},
					doFunc:	 'api_union',
				},	
				/*
GET /game/api_union.php?jsonpcallback=jsonp1326205755621&_=1326205925024&key=a34882ecc701a8d83ccc9e9e50e747e8
&id=262
&tid=1193
&info=a
&_l=en&_p=EW-DROID HTTP/1.1

jsonp1326205755621({"code":0,"ret":""})

				*/
				{ 
					cmd: 'submit_ally',
					doParam: { 'id': 262, 'tid':1193, 'info': 'wannajoin', 
								cb : function(data) {  
									self.ctx_applyAlly.code = data.code;
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(200)); }
						},
					doFunc:	 'api_union',
				}	
				],
				done_cb:function () { 
							done_cb( self.ctx_applyAlly );
					 },			// cb for complete action  	
		};
	
		this.actionSeq.setAction(actions);
		this.actionSeq.start();
	},




	 
	// donate: function(  hall_amount, techid, tech_amount, done_cb ) {
	donate: function(  amount, tech_o_ar, done_cb ) {
		var outer = this;
		var self = this;
		var cstlinfo = em.cstlmgr.getCurCstl();
		
		this.ctx_donate = {};
		
		var actions = {
			name: 'donate',
			action_table: [
/*
  // go ally 
 GET /game/api_union_info.php?jsonpcallback=jsonp1324921501938&_=1324946531329
 &key=49f84472ae46aa1956ec5ab945087e7f
 &_l=en&_p=EW-IPHONE-EN HTTP/1.1
 
jsonp1324921501938({"code":0,"ret":
{"guild":{"id":3,"name":"\ud788\ud3ec\uc12d","intro":"\uace0\uc218\ub294 \ub9d0\uc744\ud558\uc9c0\uc54a\ub294\ub2e4",
"flag":121,"score":10393,"num":50,"max":50,"state":1},
"name1":"[Hip] \ud751\uc778","name2":"[Hip] kire,[Hip] \ub8e8\ub2e4","name3":"[Hip] \ub2ec\ubaac\uc774,[Hip] eddie,[Hip] \uc11c\ub9ac,[Hip] \ub85c\uc800","name4":"","name5":"","title1":"\uc544\uac00\ub9ac\ud30c\uc774\ud130","title2":"\ub300\uc7a5","title3":"\ub300\uc7a5","title4":"\ub300\uc7a51","title5":"\ub300\uc7a52",

		<< ==   0 / 1450   ..   cooltaime : 0  
	"quota":[0,1450,0]}})
"quota":[125,700,0]}})
*/			
				{ 
					cmd: 'goto_ally_menu',
					doParam: { 
								cb : function(data) {
									// check subscribed state
									if ( ! data.ret.quota ) {
										// need to apply 
										self.ctx_donate.code  = -1;
										done_cb( self.ctx_donate );
										return;
									}
									// get quota : trans cool time 
									self.ctx_donate.trans = data.ret.quota;  
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(500)); }
						},
					doFunc:	 'api_union_info',
				},	
				/*
				 * 
	// 2.  go info  
	/api_union_info.php?jsonpcallback=jsonp1325476678147&_=1325550152864&key=362def183bdae036dc927ebc7d02600a
	&op=info
	&_l=en&_p=EW-IPHONE-EN HTTP/1.1
	
	
	jsonp1325476678147({"code":0,"ret":

				 
	[5,591308000,			<< - hall lv  
		"?",1000000,0,		// next lv  ... max   / remains 
		[[0,4,0],[0,5,0],[0,3,0],  //  civil , vetran, silos 
		
		// lv 2 
		[2,0,0],					// diligence,   
		[0,5,0],[0,5,0],				// valgor , tenacity 
		[2,0,0],
		
		
		// lv 3 				
		[2,0,0],[2,0,0],
		[2,0,0], 
		[0,4,0],[0,5,0],		// incecntive, toughness
		
		[2,0,0],[2,0,0],
		[2,0,0],
		[2,0,0],[0,5,0],			// inspiration,  battlecry
		
		// lv  
		[2,0,0],		// logistics 
		[0,3,0],[2,0,0]]]})			// bolodflag , Milicademy,  


				*/
				{ 
					cmd: 'go_info',
					doParam: { 'op': 'info',  
								cb : function(data) {  		
									// get skills 
									self.ctx_donate.hallinfo = data.ret;
									self.ctx_donate.hall_lv = data.ret[0];
									self.ctx_donate.hall_domax = data.ret[3];
									self.ctx_donate.hall_cool = data.ret[4];
									self.ctx_donate.skills = data.ret[5];
									
									// halll remains 
									if ( self.ctx_donate.hall_cool > 0 ) {
										self.ctx_donate.code  = 1;
										
										// skip hall donate... go skill donate.. 
										debug.log( self, 'Hall cool :: : ', self.ctx_donate.hall_cool, ' Try skill donate!!');
										self.actionSeq.skipNextAction();
										// done_cb( self.ctx_donate );
										// return;										
									}
									
									// check  possible  [0] != 2,  [1] < 5  
									$.each ( tech_o_ar , function(i, tech) {
										// if ( self.ctx_donate.skills[tech[0]] [0] == 0  <-- 0 �꾨땲�대룄 �� 
										if (  self.ctx_donate.skills[tech[0]] [1]  < 5 ) {
												// found 
												self.ctx_donate.techid = tech[0];		// tech id  [0]
												self.ctx_donate.tech_max = tech[1];	// max amount	[1]
											//	alert( 'tech id ' + tech[0] +' num' + tech[1]) ;
												return false; // exit each
											}
									});
									
									self.actionSeq.setDoParam('donate_hall',	{'num': self.ctx_donate.hall_domax } );
									self.actionSeq.setDoParam('donate_skill',	{'techid': self.ctx_donate.techid } );
									self.actionSeq.setDoParam('donate_skill',	{'num': self.ctx_donate.tech_max } );
									
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(500)); }
						},
					doFunc:	 'api_union_info',
				},	
				/*
	// donate hall 
	
/api_union_info.php?jsonpcallback=jsonp1325476678153&_=1325550173037&key=362def183bdae036dc927ebc7d02600a
&op=donate
&num=100
&city=15584
&_l=en&_p=EW-IPHONE-EN HTTP/1.1


jsonp1325476678153({"code":0,"ret":
[5,591308100,"?",1000000,0,
[[0,4,21594],[0,5,21594],[0,3,21594],[2,0,21594],[0,5,21594],[0,5,21594],[2,0,21594],[2,0,21594],[2,0,21594],[2,0,21594],[0,4,21594],[0,5,21594],[2,0,21594],[2,0,21594],[2,0,21594],[2,0,21594],[0,5,21594],[2,0,21594],[0,3,21594],[2,0,21594]]]})
		*/
				{ 
					cmd: 'donate_hall',
					doParam: { 'op' : 'donate', 'num': 30000, 'city': cstlinfo.id,  
								cb : function(data) {  
									// get cool time 
									self.ctx_donate.hall_cool = data.ret;  
									debug.log(self, 'halll donate :', data.ret);
									
									// check not avaliable tech donate...
									if ( ! self.ctx_donate.techid ) {
										self.ctx_donate.code = 100;
										done_cb( self.ctx_donate );
										return;
									}
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(1000)); }
						},
					doFunc:	 'api_union_info',
				},	
				/*
	// donate  skil
	
	api_union_info.php?jsonpcallback=jsonp1325476678152&_=1325550166741&key=362def183bdae036dc927ebc7d02600a
	&op=tdonate
	&num=1000000
	&techid=19
	&city=15584&
	_l=en&_p=EW-IPHONE-EN HTTP/1.1


jsonp1325476678152({"code":0,
"ret":
[[5,591308000,"?",1000000,0,
[[0,4,0],[0,5,0],[0,3,0],[2,0,0],[0,5,0],[0,5,0],[2,0,0],[2,0,0],[2,0,0],[2,0,0],[0,4,0],[0,5,0],[2,0,0],[2,0,0],[2,0,0],[2,0,0],[0,5,0],[2,0,0],[0,3,0],[2,0,0]]],

	//  cur amount, my donate,  max  ,,  cool time  		
	[3,122427508,1000000,400000000,21600]]})
				*/
				{ 
					cmd: 'donate_skill',
					doParam: { 'op': 'tdonate', 'num': 50000, 'techid': 2, 'city': cstlinfo.id,  
								cb : function(data) {  
									debug.log( self, 'skill donate: ', data.ret);
										self.ctx_donate.code = data.code; 
									if ( data.code  == 0 ) {
										self.ctx_donate.sk_ret = data.ret[1]; 
										self.ctx_donate.skill_cool = data.ret[1][4]; 
									}
									setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(200)); }
						},
					doFunc:	 'api_union_info',
				}	
				],
				done_cb:function () { 
							done_cb( self.ctx_donate );
					 },			// cb for complete action  	
		};
	
		this.actionSeq.setAction(actions);
		this.actionSeq.start();
	},



	


	start__________onShot_Fn: function() {
		
	}	,	



/*
	//////////////////////////////////////////////////////
	Fealty 
*/


	
		
	

	// cb : jquery clallbccks.. .
	doFealty: function(h, jcb) {
		var outer = this;
		var citnum = em.cstlmgr.curCstlID();
		
		function checkFealty(data) {
			jcb.fire(data);	
		}
		// use action helper
		var param = { hid: h.id, 'action': 'give',  citnum: citnum , golds: 1000, cb : function (data) {
								checkFealty.call(outer, data); } };
		em.ahelper.genmodapi_give(param);
	},
	
	 
	 /*
		 cd type :: 
		  1 : build 
		  &build_type=9 // uni :: 
		  		target : 
		  	
	 */
	
	/*
	 * 
	 /==================================
		get  : 1 shot action 
		
	*/
	/*
		GET /game/
soldier_educate_api.php?jsonpcallback=jsonp1323353477782&_=1323362536045&key=7c67cf5bf34d95dbf8c039a067a4619a
&city=22478
&_l=en&_p=EW-DROID HTTP/1.1
	*/
	// cb : jquery clallbccks.. .
	getSols: function( jcb) {
		var outer = this;
		var citnum = em.cstlmgr.curCstlID();
		
		// use action helper
		var param = { citnum: citnum ,  cb : function (data) {
								jcb.fire(data);	 } };
		em.ahelper.soldier_educate_camp(param);
	},
	
	// cb : jquery clallbccks.. .
	getHeroes: function( jcb) {
		var outer = this;
		var citnum = em.cstlmgr.curCstlID();
		
		// use action helper
		var param = { citnum: citnum , extra: 1, cb : function (data) {
								jcb.fire(data);	 } };
		em.ahelper.conscribe(param);
	},
	
	// cb : jquery clallbccks.. .
	getStudys: function( jcb) {
		var outer = this;
		var citnum = em.cstlmgr.curCstlID();
		
		// use action helper
		var param = { citnum: citnum , cb : function (data) {
								var studies = data.ret;
								em.cstlmgr.updateStudies(studies);
								jcb.fire(data);	 } };
		em.ahelper.study_api(param);
	},
	
	getEnhanTemList: function( type, jcb) {
		var outer = this;
		
		// use action helper
		var param = { 'action': 'listupdate' ,  'type': type ,cb : function(data) {
										jcb.fire(data);	 } };
		em.ahelper.goods_api_list(param);
	},

	getCitInfo: function(citid, done_cb) {
		var outer = this;
		var param = { 'citnum': citid ,
						cb :function(data) {
								if ( data.code != 0 ){
									debug.log(this, 'cbMoveCSTL_city ret error ' );
								} else {
									// update city info to cstl mgr 
									em.cstlmgr.updateCstl(data.ret.city);
									em.accmgr.updateGrade(data.ret.grade);
								}
								done_cb(data);	 
								} };
		em.ahelper.getCitInfo(param);
	},

	getCDInfo: function(citid, done_cb) {
		var outer = this;
		var paramcd = { 'citnum': citid ,  cb : function (data) {
					if ( data.code != 0 ){
						debug.log(this, 'cbMoveCSTL_cdinfo ret error ' );
					} else 	{
						// update cd list to cstl mgr 
						em.cstlmgr.updateCdlist(data.ret.cdlist);
					}
					done_cb(data); } }; 
		em.ahelper.getCDInfo(paramcd);
	},



	// check ally info 
	checkAllyInfo: function( done_cb) {
		var outer = this;
		var param = { cb : function (data) {
					done_cb(data); } }; 
		em.ahelper.api_union_info(param);
	},
		





	/*
		sequential  actions 
	*/
	
	// move cstl at index, 
	// get sols, 
	// cb : jquery clallbccks..
	userSelCstl: function(cstlidx,   param_jcb) {
		var self = this;
		
		// first move cstl : and got done cb 
		
		var dfd = $.Deferred();
		
		$.when(dfd).then(function() {		
			
				// update cur cstl to cstl mgr 
				em.cstlmgr.selCurCstl(cstlidx);
				
				// when  done: get sols  
				debug.log( self,  this, 'userSelCstl', ' after1CstlMove :: ..   : start with get Sols '	);
				
				var getsols_jcb = $.Callbacks();

				getsols_jcb.add( function (data) {
					
						debug.log(self, 'userSelCstl','sols in  ', data);
						if(data.code == 0 && data.ret ) {
							// update solders & go next step : get heroes 
							 em.cstlmgr.updateSols(data.ret.soldiers);
							 
							 // done !!
							 if ( param_jcb)
							 	param_jcb.fire(cstlidx);
							 
						} else {
							// error !! : ??? why? 
							debug.log(self, 'unexpected Error !!! : stop 1!!  ', data);
						}
						 
				});
				em.tasks.getSols(getsols_jcb);
				
			}, function(){
					alert('movectsl fail');
					}, 
			function(msg){ console.log(msg);}
		);
					
		debug.log( this, 'moveCstlAt: move to ' + cstlidx );
		// set first moved index ; 
		
		em.tasks4user.moveCstl( em.cstlmgr.getCstl(cstlidx), dfd );
		
	},
		
		
		

// 	===================================================================
//     Uni Enhance 
	// move cstl,  go uni, go wanted tap , do up 
	uniEnhance: function(cstlidx ,  id, type, done_cb) {
		var outer = this;
		var cstlinfo = em.cstlmgr.getCurCstl();
		var cur_cstlidx = em.cstlmgr.getCurCstlIdx();
		
		var target_cstlid = em.cstlmgr.getCstl(cstlidx).id; 
		var self = this;

		this.enhance = {} ;
		this.enhance.type = type ;
		this.enhance.id = id;
		this.enhance.done_cb = done_cb;
		
		// move cstl 		
		if ( cstlidx != cur_cstlidx) {

			var dfd = $.Deferred();
			$.when(dfd).then(function() {		
					
					// update cur cstl to cstl mgr 
					em.cstlmgr.selCurCstl(cstlidx);
					
					// when  done  go next :   
					debug.log( self,  this, 'uniEnhance', ' move cstl ', cstlidx	);
					
					uniEnhance();
						
					}, function(){
						alert('movectsl fail');
						// roll back index 
						em.cstlmgr.selCurCstl(cur_cstlidx);
						
						if (done_cb ) 
							done_cb(-9);
					} 
			);
						
			debug.log( this, 'moveCstlAt: move to ' + cstlidx );
			// first move index: to recv moved cstl data 
			em.cstlmgr.selCurCstl(cstlidx);
			em.tasks4user.moveCstl( em.cstlmgr.getCstl(cstlidx), dfd );
			return;
		}
				
		// cur cstl.  do right now 
		uniEnhance();
		
		function uniEnhance() {
			
			var enhance_action = {
			name: 'enhace',
			start: undefined,
			action_table: [
				// go into uni enhance 
				{ 
					cmd: 'goods_api_list',
					preAction:  undefined,
					doParam: { 'action': 'listupdate' ,  'type': 1 ,cb : function(data) { 
										outer.checkGoodsApiList.apply(outer, [data, id , 1]); }
						},
					doFunc:	 'goods_api_list',
					gotResult: outer.actionAResult,
				},
				// move to tap  sort of tem 
				{ 
					cmd: 'goods_api_list',
					preAction:  undefined,
					doParam: { 'action': 'listupdate' ,  'type': type ,cb : function(data) {
										outer.checkGoodsApiList.apply(outer, [data, id , type]); }
						},
					doFunc:	 'goods_api_list',
					gotResult: outer.actionAResult,
				},
				// do up 
				{ 
					cmd: 'goods_api_upgrade',
					preAction:  undefined,
					doParam: { citnum: target_cstlid , 'id' : id, 'action': 'upgrade', 
								cb : function(data) {  outer.checkGoodsApiUpgrade.call(outer, data); }
						},
					doFunc:	 'goods_api_upgrade',
					gotResult: outer.actionAResult,		
				},		
				// exit to cit : get cit info 
				{ 
					cmd: 'getCitInfo',
					preAction:  undefined,
					doParam: { citnum: target_cstlid ,  
								cb : function(data) {  outer.checkCitInfo.call(outer, data); }
						},
					doFunc:	 'getCitInfo',
					gotResult: outer.actionAResult,		
				},		
				],
				
			done_cb: function () { 
				done_cb( outer.enhance.result, self.enhance.tem);
				 },			// cb for complete action 
			};
		
			// remove 2nd moveing tap 
			if ( type == 1 ) {
				enhance_action.action_table.splice(1,1);
				console.log( ' leng ' , enhance_action.action_table.length );
			}
			
			self.actionSeq.setAction(enhance_action);
			self.actionSeq.start();
		};
		
	
	},

/*
	{"code":0,"ret":
{"item":[
	{"id":121379,"sid":69,"attr":[0,0,0,15,0,0],"up":0,"p":10000},
		],
	// per %, 
	"per":60,"cd":0,"up":1}})
*/
	// if tap is req tap. : check if exist wanted ite  id  
	checkGoodsApiList: function(data , id, type) {
		debug.log(this, 1, "got checkGoodsApiList :" , data );
		var self = this;
		var found = false;
		
		debug.log( self, 'watn type ', type, 'this.enhance.type: ', this.enhance.type);
		// if target tap check  tem .  
		if (this.enhance.type == type) {
			
			$.each( data.ret.item, function(i,tem){
				// debug.log( self, 'watn id ', id, 'thisid: ', tem);
				
				if ( tem.id == id ) {
					self.enhance.tem = tem;
					found = true;
					debug.log( self, tem);
					return false;	// exit each 
				}
			});

			if ( ! found ) {
				debug.log(this, 'Cant find id in this list', id, type);
				this.enhance.done_cb(-1);
				return;
			}
		}


		
		// check cool time 
		if ( data.ret.cd != 0 ) {
			debug.log(this, 'Cool Time remains', data.ret.cd);
			this.enhance.done_cb(data);
			return;
		}

		// check percent  
		var want_per = CDB.getCurUserOptions().upgradePer || 75;
		if ( data.ret.per < want_per ) {
			debug.log(this, 'Percent is low than required ', want_per , 'now: ', data.ret.per);
			this.enhance.done_cb(-3);
			return;
		}
		
		console.log(' go next 11111 ');
		setTimeout( function() { self.actionSeq.next.call(self.actionSeq); } ,CUtil.RandTime(2000));		
	},

		
	// do up &  send result  to caller 
	checkGoodsApiUpgrade: function(data) {
// 							// 0 : fail, other OK,  cool : 15m ,  cost 
// jsonp1325595647964({"code":0,"ret":[0,900,900000]})
		debug.log(this, 1, "got checkGoodsApiUpgrade :" , data );

		// store result 
		this.enhance.result = data;

		var outer = this;
		setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(2000));				
	},

	// exit to cit
	checkCitInfo: function(data) {
		debug.log(this, 1, "got checkCitInfo :" , data );
	
			// update city info to cstl mgr 
		em.cstlmgr.updateCstl(data.ret.city);
		em.accmgr.updateGrade(data.ret.grade);
		
		var outer = this;
		setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(300));				
	},


	
};

		



function CTaskUsr () {
// function em.tasks4user() {
	this.objName = 'tasks4user : ';

	var self = this;
	this.actionSeq = new CActionSeq();
	
};

CTaskUsr.prototype = {
	
	
		
	// 	===================================================================
//         start G M 

	startGM : function() {
		var self = this;
		
		var actions = {
			name: 'startGM',
			action_table: [
				{ 
					cmd: 'get_userinfo_api',
					doParam: { 	cb : function (data) {
									self.checkUserInfo.call(self, data); }
								},
					doFunc:	 'get_userinfo_api',
				},
				{ 
					cmd: 'system_task_api',
					doParam: { action: 'task_list', cb : function (data) {
									self.checkSystemTask.call(self, data); }
								},
					doFunc:	 'system_task_api',
				},		
				]
		};
		
		this.actionSeq.setAction(actions);
		this.actionSeq.start();
	},
	

	checkUserInfo : function(data) {
		
		if (data.code != 0) {
			debug.log (self, 3,  ' checkUserInfo:: ERROR !!!!! ');
			em.cbs.userlogin.fire('ERROR.startGM', 'error key!!! ');
			return;
		}
		// call cb user info 
		em.cbs.userlogin.fire('DATA.userinfo', data.ret);
		
		var outer = self;
		this.actionSeq.donextTimeout( 500); 
	} ,
	/*
			{"quest":[
		{"id":48,"grade":0,"status":0,"done":1},
		{"id":49,"grade":0,"status":1,"done":1},
	*/
	checkSystemTask : function(data) {
		var outer = this;
		// update acc mgr 
		em.cbs.userlogin.fire('DATA.questinfo', data.ret);
		this.actionSeq.donextTimeout( 500); 
	},
	



		// 	===================================================================
//         Login New User 
	

	// 癒쇱� select user 瑜���寃껋쑝濡�媛�젙... 
	// assume : before  select user is done.. 
	processLogin :function(username, pw) {
		var outer = this;
		
		var curuser_info =  CDB.getCurUserInfo();
		this.caller = arguments.callee.caller;
		this.cb = em.cbs.userlogin;
		 
		 // test :	 /// call cb result 
 		 // em.cbs.userlogin.fireWith(arguments.caller , ['start login cb msg']);
	
		var bPvp = 0;
		//  greate pvp  임시 처리
		if ( username == 'greate_PVP' ) { 
			username = 'Greateload';
			bPvp = 1;
			
		}
		
		// only for   ::  try login ::  
		if ( pw != undefined) {
			curuser_info = curuser_info || {};
			curuser_info.password = pw;
			curuser_info.device_code = 'e7e8b613d1b08a603924563cb2c3ea24fd23fc2e';
			debug.log( this, ' pass :', pw, ' set: ', curuser_info.password , curuser_info.device_code);
		}
		/*
			GET /info.php?
jsonpcallback=jsonp1323389966359&_=1323389984249
&user=Greateload
&action=login
&pvp=0
&code=e7e8b613d1b08d069751171da8b0eb17fe02ee8c				// iphone kr 

		*/
		var login_action = {
			name: 'Login',
			start: undefined,
			param: this.task_cbs,		// for cb functions 
			action_table: [
				{ 	
					cmd: 'login_info',
					preAction:  undefined,
					doParam: { user: username , login_action: 'login', pvp: bPvp, 
									device_code: curuser_info.device_code,
									nosessionkey: true, 
									cb : function (data) {
										checkLoginInfo.call(outer, data); }
								},
					doFunc:	 'login_info',
					gotResult: this.actionAResult,
				},
				
/*
	GET /game/
login_api.php?
jsonpcallback=jsonp1323389966360&_=1323389985557
&username=Greateload
&password=1212
&_l=kr&_p=EW-IPHONE-KR HTTP/1.1
		
		
jsonp1323389966360(
	{"code":0,"ret":{"key":"b557990e3591188901cb50f1422bc0d4"}})
*/				
				{ 
					cmd: 'login_api',
					preAction:  undefined,
					// doParam: { username: curuser_info.name, password: curuser_info.password, nosessionkey: true, cb : function (data) {
					doParam: { username: username, password: curuser_info.password, nosessionkey: true, cb : function (data) {
									checkLoginApi.call(outer, data); }
								},
					doFunc:	 'login_api',
					gotResult: this.actionAResult,		// �대뵒��?? 
				},		
				]
		};
	
		
		this.actionSeq.setAction(login_action);
		this.actionSeq.start();
},
	

	
	checkLoginInfo : function(data) {
			// {"code":0,"ret":{
		// "server":"http:\/\/k2.emrosswar.com\/",
		// "user":"Greateload","referer":"feelcool","refercode":"Greateload"}})
		debug.log(this,CDB.getCurUser(), data);

 		 
		if ( data.code != 0 ) {
			debug.log(self, 3, ' LOGIN CB Data Error !!! ');
			em.cbs.userlogin.fire('ERROR.LOGIN_INFO', data);
			return;
		}

		// call new user with server 
		em.cbs.userlogin.fire('DATA.server', data.ret.server);
				
		if ( CDB.getCurUser() != data.ret.user) {
			// just warning !!!
			debug.log(self, 3, '  !!! Not Match User Name !!! ', CDB.getCurUser() ,  data.ret.user);
			// return;
		}
		var outer = self;
		setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(500));
	},
	
	
	checkLoginApi : function(data) {
		// {"code":0,"ret":{"key":"b557990e3591188901cb50f1422bc0d4"}})
		var outer = self;
		
		if (data.code != 0) {
			debug.log (self, 3,  ' CB data got  checkLoginApi:: ERROR !!!!! ');
			em.cbs.userlogin.fire('ERROR.LOGIN_API', data);
			return;
		}
		
		// call new user with new key 
		em.cbs.userlogin.fire('DATA.key', data.ret.key);
		
		debug.log (self, 3,  ' CcheckLoginApi:: Got New Key :: Store !!!!!!! ' + data.ret.key);
		em.util.print_result(' CcheckLoginApi:: Got New Key :: Store !!!!!!! ' + data.ret.key);
		
		
				// test :	 /// call cb result 
 		// this.cb.fireWith(this.caller , ['got key']);
 		
		//  start : start user fucntio 
		// setTimeout( function() { outer.actionSeq.next.call(outer.actionSeq); } ,CUtil.RandTime(2000));
		setTimeout( function() { outer.startGM.call(outer); } ,CUtil.RandTime(500));
	},
	
	
	
// 	===================================================================
//          move next cstl 
			/*
			jsonp1323389987756({"code":0,"ret":{"cdlist":[
{"id":843558,"cdtype":1,"target":9,"owner":0,"secs":17539},    //  uni  
{"id":846316,"cdtype":1,"target":10,"owner":0,"secs":69911},	// arena 
{"id":847570,"cdtype":1,"target":13,"owner":0,"secs":26386},   	// fuci
{"id":220951,"cdtype":2,"target":7,"owner":87575,"secs":75168}, //   study  2 ex Arm  7  h id 
{"id":192011,"cdtype":3,"target":15,"owner":152,"secs":4111}    // train  o   3   gid  
],"grade":42,"money":345}})	
		*/
/*
	var checkCDlist =function(data) {

		console.log(data);
		
		// store cd list to  0 th cstl  
		var cstls = CDB.getCurUserCtsl();

		// replace 占쎈똻鍮�占쏙옙 
		cstls[0].cdlist = data.ret.cdlist;
		// $.extend(cstls[0] , data.ret);
		 
	};
	*/

	
	// ===============================================
	//  move cstl
	// not a seq action..  
	moveCstl :function(target_cstl, dfd) {
		
		this.done_step = 2;
		this.cur_step = 0;
		
		// set dfd 
		this.cstl_dfd = dfd;
		
		debug.log( this, 'moveCstl : target_cstl id : ' + target_cstl.id );
		// use action helper
		var outer = this;
		var paramcity = { citnum: target_cstl.id ,  cb : function (data) {
								cbMoveCSTL_city.call(outer, data); } };
		em.ahelper.getCitInfo(paramcity);
		var paramcd = { citnum: target_cstl.id ,  cb : function (data) {
								cbMoveCSTL_cdinfo.call(outer, data); } }; 
		em.ahelper.getCDInfo(paramcd);

	},
	
	cbMoveCSTL_city :function(data) {
		if ( data.code != 0 ){
			// error !!! 
			debug.log(this, 'cbMoveCSTL_city ret error ' );
			this.cstl_dfd.reject();
			return;
		}

		// update city info to cstl mgr 
		em.cstlmgr.updateCstl(data.ret.city);
		em.accmgr.updateGrade(data.ret.grade);
		// chgeck action compolete		
		if ( ++this.cur_step == this.done_step) {
			this.cstl_dfd.resolve();
		}

	},
	
	cbMoveCSTL_cdinfo :function(data){
		if ( data.code != 0 ){
			// error !!!
			debug.log(this, 'cbMoveCSTL_cdinfo ret error ' );
			this.cstl_dfd.reject();
		} else 
		{
			
		
			// update cd list to cstl mgr 
			em.cstlmgr.updateCdlist(data.ret.cdlist);
			
			if ( ++this.cur_step == this.done_step) {
				this.cstl_dfd.resolve();
			}
			
			// run registered after action 
			// this.move1done_dfd.resolve(data);
			
			// em.hntmgr.fireEvent('cstlchanged');
			
		}
	}
	
	


};